Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between setReadTimeout() and setConnectTimeout()?

I am sorry if this sounds a very dumb question but googling around for quite sometime did not yield the answer for this question.

I am trying to set the timeout for a connection I am making to a server, where I am using HttpComponentsClientHttpRequestFactory and trying to set the timeout as follows:

clientHttpRequestFactory.setConnectTimeout(timeout);

However, I see that there is another method setReadTimeout(), so I am not sure which is the right one to use to set a timeout before I get a response from the server. The documentation on HttpComponentsClientHttpRequestFactory isn't very clear on this.

Can someone please explain the difference between these two methods?

Edit: What I thought was, if the network is unreliable, I should be setting setConnectTimeout() and setReadTimeout() should be set when the server is unreliable. Is that correct?

like image 305
jobin Avatar asked Nov 02 '15 14:11

jobin


People also ask

What is setConnectTimeout?

setConnectTimeout. public void setConnectTimeout(int timeout) Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.

What is the difference between Readtimeout and Connecttimeout?

The connection timeout is the timeout in making the initial connection; i.e. completing the TCP connection handshake. The read timeout is the timeout on waiting to read data1.

What is the difference between connection timeout and socket timeout?

connection timeout — a time period in which a client should establish a connection with a server. socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.

What is read timeout in HTTP?

The HTTP read timeout determines how long the IBM® Watson IoT Platform - Message Gateway Web UI waits before it times out when it is waiting for an HTTP read request to complete. By default, this value is set to 20 seconds.


1 Answers

ConnectTimeout is the timeout for creating a connection. Let's say you have an unreliable server and you want to wait only 15 seconds before you tell the user that "something is wrong".

ReadTimeout is the timeout when you have a connection, you're blocked on read() and you want to get an exception if the read blocks for more than timeout.

Real life examples would be for example to check whether a particular network site is up. The only reliable way to test that is to try to connect to it. You might be able to connect to it, it might give you connection denied, or it would just hang at connection due to network issues. That's where a connection timeout is handy.

A read timeout can be useful in an application protocol where the clients are required to send a "heartbeat" every so often, to let them know they're still connected (if the server regularly writes back to the client, this isn't necessary, but that's not always the case). You would use a read timeout of the heartbeat time (plus some extra), and if it times out then you can assume the client has been disconnected and close the socket.

like image 176
Kayaman Avatar answered Sep 21 '22 10:09

Kayaman