我正在JavaFx上对EMAIL客户端服务器进行编码。这是ServerThread代码:
public void run() {
System.out.println("---SONO NEL THREAD---");
ObjectInputStream inStream = null;
ObjectOutputStream outStream = null;
try {
System.out.println("-- CONNESSO --");
System.out.println("Socket:" + socket);
InputStream sStream = socket.getInputStream();
System.out.println("Socket Stream: " + sStream);
inStream = new ObjectInputStream(sStream);
System.out.println("Input Stream: " + inStream);
List<Mail> mailList = (List<Mail>) inStream.readObject();
System.out.println(mailList);
try {
FileWriter w, log;
System.out.println("Server: crea Log");
log = new FileWriter("log.txt", true);
System.out.println("List Size:" + mailList.size());
if (mailList != null && mailList.size() > 0) {
System.out.println("---SERVER: LIST---");
for (Iterator iterator = mailList.iterator(); iterator.hasNext();) {
System.out.println(iterator.hasNext());
Mail mail = (Mail) iterator.next();
System.out.println("List:" + mail.toString());
System.out.println("Author: " + mail.getSender());
if (mail.getStatus().equals("toRead")) {
System.out.println("---");
}
}
}
// useless code
} catch (IOException e) {
System.out.println("Exception into Server Thread, internal");
e.printStackTrace();
}
} catch (EOFException ex) {
System.out.println("client closed");
} catch (IOException ex) {
System.out.println("Exception into Server Thread, external IO");
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Exception into Server Thread, external NotFound");
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
//ex.printStackTrace();
}finally{
try {
if(inStream != null){
inStream.close();
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("Exception into Server Thread, socket");
System.out.println(e.getMessage() + "IO2!");
}
}
}
System.out.println("stream chiuso");
} catch (IOException ex) {
System.out.println("Exception into Server Thread, stream");
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
当我关闭客户端时,我在服务器上收到以下错误消息:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2663)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2679)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
at email.ServerThread.run(ServerThread.java:61)
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2663)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2679)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
at email.ServerThread.run(ServerThread.java:61)
错误email.ServerThread.run(ServerThread.java:61)
指向inStream = new ObjectInputStream(sStream);
我认为这是有关“退出”的问题,我的意思是当我按客户端上的“退出”按钮时,ObjectInputStream没有关闭,这给了我这个错误。但是我真的不知道如何解决或检查这种问题。