对不起,如果这是另一个问题的转贴;经过一个小时的谷歌搜索,我找不到任何东西。无论如何,问题是:我在游戏中有一个服务器类和一个客户端类。服务器应该通过网络向客户端发送一个包含所有信息的对象。但是,当我尝试发送它时,它给了我一个 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)
所以真的,长话短说,我搞砸了一些愚蠢的事情(可能),现在对象不会在客户端和服务器之间发送。任何修复的想法?谢谢!