Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataInputStream readByte blocked / hanging

Tags:

java

sockets

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();
like image 515
phobus Avatar asked Jul 25 '26 01:07

phobus


1 Answers

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!");
}       
like image 178
WhiteFang34 Avatar answered Jul 27 '26 15:07

WhiteFang34



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!