开发者

Socket with timeout set sometimes stalls on reading

I have a Java client (1.6b17 on windows XP, launched via Java webstart) that uses a TCP socket to query a Java based server over an ADSL connetion. The client socket has a timeout set (3000ms).

Occasionally (and so far, not reproducibly), the client stalls while reading responses from the server. There are a few hundred installations and most users don't encounter the issue most of the time.

The client creates a new socket for each request following the steps below:

  1. Create a new socket setting the timeout to be 3000ms
  2. Submit request (a single line of text)
  3. Read multiple lines of response, closing the socket when a special "end of message" sequence is received (or a null)

Here's some (simplified) code to illustrate:

socket = new Socket(host, port);
socket.setSoTimeout(socketTimeout);

out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

socket.write(cmd);

while (true) {

   line = in.readLine();

   if (line == null) break; 

   if (line.equals("EOL")) break;

   // Store line of text in a开发者_StackOverflow中文版n ArrayList
}    

socket.close();

The problem is that occasionally, in the middle of receiving, the client will pause for up to 5 minutes and then resume without any intervention. Following this, the application resumes with no noticable performance issues.

Logging on the server side indicates that the operation takes approx 150ms although the timestamp in question is taken prior to closing the socket (something I will try to address).

One of my working assumptions is that this is not due to network issues as otherwise, the readLine would timeout, is this a valid assumption?

It also doesn't seem likely that this would be garbage collection related. The app has 512MB allocated to the heap while the data being retrieved consists of approx 1600 lines of 100 characters. My expectation is if the problem related to GC or memory leaks that there would be a general degradation in performance rather than the sort of "sticky" behaviour that I am seeing, is this correct?

Any suggested strategies for troubleshooting this would be greatly appreciated.

Thanks, Phil


I didn't see you get up to 5 minute "pauses". I can only imagine you are getting lots of retries on packets. You should be able to see something like this with wireshark. It can give you a clue if its something else as well.

Your full GC pause time on a 512 MB heap should be around 0.5 seconds (very approximately)


If you have an unreliable connection I wouldn't suggest using PrintWriter. If it gets an IOException, it sets a flag rather than throwing an exception. If you continue to use it, it does nothing.


The readLine() should timeout. Never seen that not work, on any platform. Maybe you are only receiving one character at a time, at intervals less than 3s. Wireshark will tell you.

Or as you are reading lines, maybe the server isn't sending a newline in the correct places?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜