如果我结束客户端会话,则会引发 EOFException,但我读到这很正常,所以我只需启动一个具有相同功能但值为 Restaurant=null; 的新线程。虽然我把它写在一个 .txt 文件中
public void run(){
try {
ois= new ObjectInputStream(clientside.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos = new ObjectOutputStream(clientside.getOutputStream());
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
handlerequest(ois,oos);// exit();
}
我认为您不需要我的请求处理程序的代码,所以我不会附加它以减少代码垃圾邮件。调用以下方法的方法是 requesthandler
String tempRestaurant=null;
try {
fr = new FileReader("Restaurant.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fr.read(cbuf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tempRestaurant=String.valueOf(cbuf);
System.out.println(tempRestaurant);
try {
oos.writeObject(tempRestaurant);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果您需要更多信息代码等,我期待您的帮助。 :) 这是退出客户端后抛出的下面的异常
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at prealphaserverpackage.clientsidethreads.setRestaurant(Serverpart.java:164)
at prealphaserverpackage.clientsidethreads.handlerequest(Serverpart.java:205)
at prealphaserverpackage.clientsidethreads.run(Serverpart.java:96)
Exception in thread "Thread-3" java.lang.NullPointerException
at java.io.PrintWriter.write(Unknown Source)
at prealphaserverpackage.clientsidethreads.setRestaurant(Serverpart.java:177)
at prealphaserverpackage.clientsidethreads.handlerequest(Serverpart.java:205)
at prealphaserverpackage.clientsidethreads.run(Serverpart.java:96)
我将知道你的代码添加到我的代码中,但在抛出 EOFException 后餐厅仍然为空......