I'm looking at the code for a server that creates a thread to handle each incoming connection. The problem is that for some reason on a bunch of threads, the DataInputStream created from the socket is hanging on readByte and not throwing any exceptions. The timeout is set for 60 seconds, so I'm not sure what the next step is with this.
socket.setSoTimeout(timeout);
socketInputStream = socket.getInputStream();
byte connectionOptions = socketDataInputStream.readByte();
You indicated in a comment that you're waiting for a SocketException. If so you need to catch SocketTimeoutException instead. For example, this code will output timeout! if you telnet to port 3434 and wait 3 seconds:
try {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
socket.setSoTimeout(3000);
InputStream socketInputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(socketInputStream);
dataInputStream.readByte();
} catch (SocketTimeoutException e) {
System.out.println("timeout!");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With