Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Socket InputStream read() unblock if server times out?

Tags:

java

sockets

I have a server that times out after 45 seconds if it hasn't received a full request and closes the connection. I connect to this server through a Socket and write my request to the socket's OutputStream.

Socket socket = new Socket("myhost", myPort);
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.write(properRequestMessage);
out.flush();

I'm assuming here that my request is good (follows my protocol). The server is supposed to respond with a file. I try to read from the socket inputstream:

BufferedReader response = new BufferedReader(new InputStreamReader(socket.getInputStream()));

String in;

while((in = response.readLine()) != null) {
    System.out.println(in);
}

The readLine() blocks here and I think it is because my server thinks my request isn't properly terminated and is therefore waiting for more.

Now, if 45 seconds pass and my server times out, will the readLine() unblock or wait for some Socket default timeout time?

like image 941
Sotirios Delimanolis Avatar asked Feb 19 '23 03:02

Sotirios Delimanolis


1 Answers

That depends on what the server does when it times out. If it closes the connection you will see that. If it just logs a message, you might not see anything.

There is no default read timeout. Your readLine() can wait forever.

like image 88
Peter Lawrey Avatar answered Mar 15 '23 23:03

Peter Lawrey