Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the functionality of setSoTimeout and how it works?

I'm trying to learn Socket by myself. I'm little bit confused by following text from Oracle website. I have some questions regarding that. Thanks in advance for any clear explanation.

setSoTimeout

public void setSoTimeout(int timeout) throws SocketException

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. 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. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. 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.

  1. What is SO_TIMEOUT?
  2. Socket is the endpoint of a connection. If I say

    mySocket.setSoTimeout(2000); 

    Does it mean that I'm blocking reading any input from the Server/Client for this socket for 2000 millisecond and after this time the socket is ready to read data?

  3. What does it mean timeout expire?

  4. What is the option which must be enabled prior to blocking operation?

  5. Infinite Timeout means that the socket does't read anymore?

like image 737
Bernard Avatar asked Oct 10 '12 13:10

Bernard


People also ask

What is setSoTimeout?

The setSoTimeout () method of Java Socket class enables or disables the SO_TIMEOUT option with the given timeout value, in milliseconds. The timeout value should be greater than 0 otherwise, it will throw an error.

How do I fix socket timeout exception?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

What is the default Java socket timeout?

timeout - The socket timeout value passed to the Socket. setSoTimeout() method. The default on the client side is 1800000 milliseconds (30 minutes).


2 Answers

Does it mean that I'm blocking reading any input from the Server/Client for this socket for 2000 millisecond and after this time the socket is ready to read data?

No, it means that if no data arrives within 2000ms a SocketTimeoutException will be thrown.

What does it mean timeout expire?

It means the 2000ms (in your case) elapses without any data arriving.

What is the option which must be enabled prior to blocking operation?

There isn't one that 'must be' enabled. If you mean 'may be enabled', this is one of them.

Infinite Timeout menas that the socket does't read anymore?

What a strange suggestion. It means that if no data ever arrives you will block in the read forever.

like image 180
user207421 Avatar answered Oct 11 '22 09:10

user207421


The JavaDoc explains it very well:

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. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. 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.

SO_TIMEOUT is the timeout that a read() call will block. If the timeout is reached, a java.net.SocketTimeoutException will be thrown. If you want to block forever put this option to zero (the default value), then the read() call will block until at least 1 byte could be read.

like image 31
logoff Avatar answered Oct 11 '22 08:10

logoff