これが別の質問の再投稿である場合は申し訳ありません。1時間グーグルした後、何も見つかりませんでした。とにかく、ここに問題があります:私はゲームにサーバークラスとクライアントクラスを持っています。サーバーは、すべての情報を含むオブジェクトをネットワーク経由でクライアントに送信する必要があります。ただし、送信しようとすると、IO.EOFExceptionが発生します。すべてのコードは次のとおりです。
public class NetworkClient {
private static Socket socket = null;
private static ObjectOutputStream oos;
private static ObjectInputStream ois;
private static FileInputStream fileIn;
public static void startConnection(){
try {
socket = new Socket("192.168.1.5", 4445);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: 192.168.1.5.");
}
fileIn = null;
}
public static void getVariables(){
try
{
FileInputStream fileIn =
new FileInputStream("variables.ser");
ois = new ObjectInputStream(fileIn);
Display.variables = (Variables) ois.readObject();
ois.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Variables class not found");
c.printStackTrace();
return;
}
}
}
そして、これがサーバーです:
public class NetworkHost {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static ObjectInputStream ois;
private static ObjectOutputStream oos;
public static void startConnection(){
serverSocket = null;
try {
serverSocket = new ServerSocket(4445);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
try {
ois = new ObjectInputStream(clientSocket.getInputStream());
oos = new ObjectOutputStream(clientSocket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void sendVariables(){
try
{
FileOutputStream fileOut =
new FileOutputStream("variables.ser");
ObjectOutputStream out =
new ObjectOutputStream(fileOut);
out.writeObject(Display.variables);
out.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
}
}
そして最後に、ここにエラーがあります:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at com.dodger.NetworkClient.getVariables(NetworkClient.java:36)
at com.dodger.Display.main(Display.java:129)
つまり、簡単に言えば、私は(おそらく)愚かなことを台無しにしてしまい、オブジェクトがクライアントとサーバーの間で送信されなくなりました。修正のアイデアはありますか?ありがとう!