I have written a small program in java, which reads from a socket inputstream in an extra thread, this is the run-method of the reading thread, currently I run two of them, one for a test-client and one for a test-socket, both on my laptop (localhost).
@Override
public void run() {
try {
while (!this.isInterrupted() && !this.connection.isClosed()) {
//read some data, store them into a byte[] and pass them to a processing method
}
} catch (IOException e) {
e.printStackTrace(); //TODO Logger
}
}
I tested the code and got a SocketClosedException. After testing and running it over ten times again, I did not get any exception or similar. For my understanding, the read(...) method blocks until I read something right? So how can I escape the thread, if the inputstream is closed? Is there even any need? Like I said, I was not able to reproduce the exception again.
EDIT: I managed to create the exception again. I got:
java.net.SocketException: Socket closed
and after that
java.net.SocketException: Connection reset
Any ideas how to prevent them?