Hello there,
The documentation of Socket.setSoTimeout states:
+"...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..."+. Simple enough, but I dont understand it's significance. Let me explain, I did a small test using the following code:
public static void main( String[] args )
{
Socket s = new Socket("72.5.124.111", 80 );
s.setSoTimeout(9000);//9 seconds
InputStream is = s.getInputStream();
while( is.read(buffer) != -1 )
System.out.println(amountRead);
}
As you can see, I am waiting for a response from an HTTP server, even though I have not sent any request to it. Clearly this code is stupid and as expected, I get a SocketTimeoutException after 9 seconds.
However I remove the s.setSoTimeout() line... I get an error from the native environment exactly after 60 seconds (I counted using a stop watch!).
Now we know that Socket works on TCP/IP, and therefore retries and timeouts are handled by the protocol itself. So my question is... why would anyone need setSoTimeout()? Is it a mistake to set the Timeout less than TCP's default timeout (in my case it seems it was 60 seconds)?
Edited by: GizmoC on Apr 28, 2008 2:20 AM