简体   繁体   English

如果服务器超时,Socket InputStream read()是否会解锁?

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

I have a server that times out after 45 seconds if it hasn't received a full request and closes the connection. 如果没有收到完整请求并关闭连接,我有一台服务器在45秒后超时。 I connect to this server through a Socket and write my request to the socket's OutputStream . 我通过Socket连接到这个服务器,并将我的请求写入套接字的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. readLine()阻塞在这里,我认为这是因为我的服务器认为我的请求没有正确终止,因此等待更多。

Now, if 45 seconds pass and my server times out, will the readLine() unblock or wait for some Socket default timeout time? 现在,如果45秒后通过并且我的服务器超时, readLine()解锁或等待某个Socket默认超时时间?

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. readLine()可以永远等待。

如果服务器在该超时时关闭其套接字的末尾,则readLine()将返回null

The readLine() method will block until it receives an input or until the underlying socket read() timeout ends. readLine()方法将阻塞,直到它收到输入或直到底层套接字read()超时结束。 You don't set the timeout on the read command but rather on the socket it self. 您没有在读取命令上设置超时,而是在自己的套接字上设置超时。

Socket.setSoTimeout(int ms) . Socket.setSoTimeout(int ms)

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. 使用指定的超时启用/禁用SO_TIMEOUT,以毫秒为单位。 With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. 如果将此选项设置为非零超时,则与此Socket关联的InputStream上的read()调用将仅阻止这段时间。 If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. 如果超时到期,则引发java.net.SocketTimeoutException,尽管Socket仍然有效。 The option must be enabled prior to entering the blocking operation to have effect. 必须在进入阻止操作之前启用该选项才能生效。 The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout. 超时必须> 0.超时为零被解释为无限超时。

What actually occurs also depends on what the server does, if it closes the socket correctly a IOException should be thrown by readLine() . 实际发生的还取决于服务器的作用,如果正确关闭套接字,则readLine()应抛出IOException If the connection isn't close it will wait for the socket to timeout. 如果连接未关闭,它将等待套接字超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM