Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Signifiance of setSoTimeout() ?

843790Apr 28 2008 — edited Apr 28 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 26 2008
Added on Apr 28 2008
5 comments
293 views