You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Julien Feltesse <ju...@amalto.com> on 2006/07/17 17:33:09 UTC

java.net.SocketTimeoutException: Read timed out

Hi,


I can't explain to myself an annoying bug I've got in my http server & 
client code.
The server itself is the dumbest http server you can make. A class that 
listens to a port and spawns threads to handle the incoming connections.
The client is of course using httpclient and there too, nothing too 
complicated.
But,
I've got a bug I can't find a way to sweep out of the code. The tricky 
thing is, it occurs randomly. I'm using the same code, I'm sending the 
same message but somtimes it works, sometimes it hangs.
It hangs when receiving the message. Here's the code that reads the 
request:


ByteArrayOutputStream body = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int count = 0;
int totalCount = 0;

while ( (totalCount < contentLength) && ((count = 
inputStream.read(buffer)) > 0) && (! this.kill) ) {

    Logger.getLogger(this.getClass()).debug("run() : read    "+count+" 
bytes. total="+totalCount);
    totalCount += count;
    body.write(buffer, 0, count);
    Logger.getLogger(this.getClass()).debug("run() : written "+count+" 
bytes. total="+totalCount);

}


And the trace I've got when things work fine :

run() : read    2048 bytes. total=0
run() : written 2048 bytes. total=2048
run() : read    2048 bytes. total=2048
run() : written 2048 bytes. total=4096
run() : read    2048 bytes. total=4096
run() : written 2048 bytes. total=6144
run() : read    2048 bytes. total=6144
run() : written 2048 bytes. total=8192
run() : read    778 bytes. total=8192
run() : written 778 bytes. total=8970


However, sometimes I run the server and the client and here's what happens:

run() : read    1326 bytes. total=0
run() : written 1326 bytes. total=1326
/**** the program hangs here until timeout then it throws: ****/

java.net.SocketTimeoutException: Read timed out
   at java.net.SocketInputStream.socketRead0(Native Method)
   at java.net.SocketInputStream.read(SocketInputStream.java:129)
   at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
   at java.io.BufferedInputStream.read(BufferedInputStream.java:201)
   at 
org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
   at 
org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
   at 
org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115) 

   at 
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.readLine(MultiThreadedHttpConnectionManager.java:1373) 

   at 
org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832) 

   at 
org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590) 

   at 
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995) 

   at 
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397) 

   at 
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170) 

   at 
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
   at 
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)


I really don't know have a clue so if anyone had an advice or some 
previous experience of this issue, I'd be glad to hear from you.


Thanks,

Julien


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: java.net.SocketTimeoutException: Read timed out

Posted by Roland Weber <ht...@dubioso.net>.
Hello Julien,

> The server itself is the dumbest http server you can make. A class that
> listens to a port and spawns threads to handle the incoming connections.
>
> while ( (totalCount < contentLength) && ((count =
> inputStream.read(buffer)) > 0) && (! this.kill) ) {
>
> /**** the program hangs here until timeout then it throws: ****/

Don't rely on the content length sent by a server. In particular not
if it's a handcrafted dumb server that is most likely to screw up.
Read to the end of the stream.

cheers,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


RE: java.net.SocketTimeoutException: Read timed out

Posted by "Paradkar, Ami S." <am...@gs.com>.
Hi Juliean
I had similar problem in my code when I reading one line at a time using
readLine(). After trying various methods I tried reading 8k byte at a
time.This did not hang for me but I still could not answer as to why is
use to hang for the other methods. 


Ami

-----Original Message-----
From: Julien Feltesse [mailto:julien.feltesse@amalto.com] 
Sent: Monday, July 17, 2006 11:33 AM
To: httpclient-user@jakarta.apache.org
Subject: java.net.SocketTimeoutException: Read timed out

Hi,


I can't explain to myself an annoying bug I've got in my http server & 
client code.
The server itself is the dumbest http server you can make. A class that 
listens to a port and spawns threads to handle the incoming connections.
The client is of course using httpclient and there too, nothing too 
complicated.
But,
I've got a bug I can't find a way to sweep out of the code. The tricky 
thing is, it occurs randomly. I'm using the same code, I'm sending the 
same message but somtimes it works, sometimes it hangs.
It hangs when receiving the message. Here's the code that reads the 
request:


ByteArrayOutputStream body = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int count = 0;
int totalCount = 0;

while ( (totalCount < contentLength) && ((count = 
inputStream.read(buffer)) > 0) && (! this.kill) ) {

    Logger.getLogger(this.getClass()).debug("run() : read    "+count+" 
bytes. total="+totalCount);
    totalCount += count;
    body.write(buffer, 0, count);
    Logger.getLogger(this.getClass()).debug("run() : written "+count+" 
bytes. total="+totalCount);

}


And the trace I've got when things work fine :

run() : read    2048 bytes. total=0
run() : written 2048 bytes. total=2048
run() : read    2048 bytes. total=2048
run() : written 2048 bytes. total=4096
run() : read    2048 bytes. total=4096
run() : written 2048 bytes. total=6144
run() : read    2048 bytes. total=6144
run() : written 2048 bytes. total=8192
run() : read    778 bytes. total=8192
run() : written 778 bytes. total=8970


However, sometimes I run the server and the client and here's what
happens:

run() : read    1326 bytes. total=0
run() : written 1326 bytes. total=1326
/**** the program hangs here until timeout then it throws: ****/

java.net.SocketTimeoutException: Read timed out
   at java.net.SocketInputStream.socketRead0(Native Method)
   at java.net.SocketInputStream.read(SocketInputStream.java:129)
   at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
   at java.io.BufferedInputStream.read(BufferedInputStream.java:201)
   at 
org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
   at 
org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
   at 
org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.jav
a:1115) 

   at 
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpCon
nectionAdapter.readLine(MultiThreadedHttpConnectionManager.java:1373) 

   at 
org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBa
se.java:1832) 

   at 
org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase
.java:1590) 

   at 
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java
:995) 

   at 
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMe
thodDirector.java:397) 

   at 
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMetho
dDirector.java:170) 

   at 
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
96)
   at 
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
24)


I really don't know have a clue so if anyone had an advice or some 
previous experience of this issue, I'd be glad to hear from you.


Thanks,

Julien


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org