I have just started working with sockets and I am not a very experienced programmer. I would like to close the connection on client side by the use of a disconnect button. The disconnect button would execute the following code:
private void closeConnection() {
if (socket != null) {
jLabelStatus.setText("Status: closing connection");
try {
socket.close();
} catch (IOException ex) {
Logger.getLogger(DatabaseClient.class.getName()).log(Level.SEVERE, null, ex);
} finally {
socket = null;
}
}
}
However, I am not very sure how I can assure that no exceptions turn up on the server side. My run()
method (which overrides Runnable run()
method) executes the following code as part of finally block on catching an exception when trying to read messages:
try {
threadSays("Lost connection to client.");
socket.close();
} catch (IOException ex) {
Logger.getLogger(ClientHandlerThread.class.getName()).log(Level.SEVERE, null, ex);
}
Here is when, if I do press the disconnect button, the server throws a SEVERE: null java.io.EOFException.
I understand that this might be because... I am trying to do socket.close()
here, even though it's been done in the client. So what would be the best way to assure no exception occurs? Is the socket.close()
method not required if socket.close()
occurs in the client? How do I check if it has occurred?
Edit: Stack Trace
SEVERE: null
java.io.EOFException
at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3062)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1561)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
at sqlitechinookcw.ClientHandlerThread.run(ClientHandlerThread.java:64)
at java.base/java.lang.Thread.run(Thread.java:830)