Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.SocketTimeoutException: Read timed out

I have an application with client server architecture. The client use Java Web Start with Java Swing / AWT and the sert uses HTTP server / Servlet with Tomcat. The communication is made from the serialization of objects, create a ObjectOutput serializes a byte array and send to the server respectively called the ObjectInputStream and deserializes.

The application follows communicating correctly to a certain time of concurrency where starting to show error "SocketException read timeout". The erro happens when the server invoke the method ObjectInputStream.getObject() in my servlet doPost method.

The tomcat will come slow and the errors start to decrease server response time until the crash time where i must restart the server and after everything works.

Someone went through this problem ?

Client Code

URLConnection conn =  url.openConnection();
conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();
ObjectOutputStream oss = new ObjectOutputStream(os);

oss.writeUTF("protocol header sample");

oss.writeObject(_parameters);
oss.flush();
oss.close();

Server Code

ObjectInputStream input = new ObjectInputStream(_request.getInputStream());
String method = input.readUTF();

parameters = input.readObject();

input.readObject() is where the error is

like image 505
Rafael Soto Avatar asked Mar 20 '10 02:03

Rafael Soto


People also ask

How do I fix Java net SocketTimeoutException read timed out?

A possible solution for this problem within the Tomcat web application is to modify the CONTEXT. XML file, and modify the CONNECTOR definition that governs the workstation browser connectivity to the Tomcat server. Specifically, modify the connectionTimeout value. Increase this value to suppress the error condition.

What is SocketTimeoutException read timeout?

Server is trying to read data from the request, but its taking longer than the timeout value for the data to arrive from the client. Timeout here would typically be Tomcat connector → connectionTimeout attribute.

What causes Java SocketTimeoutException?

If the timeout elapses before the method returns, it will throw a SocketTimeoutException. Sometimes, firewalls block certain ports due to security reasons. As a result, a “connection timed out” error can occur when a client is trying to establish a connection to a server.


1 Answers

You haven't given us much information to go on, especially about the client side. But my suspicion is that the client side is:

  • failing to setting the Content-length header (or setting it to the wrong value),
  • failing to flush the output stream, and/or
  • not closing the output side of the socket.

Mysterious.

Based on your updated question, it looks like none of the above. Here are a couple of other possibilities:

  • For some reason the client side is either locking up entirely during serialization or taking a VERY LONG TIME.
  • There is a proxy between the client and server that is causing problems.
  • You are experiencing load-related network problems, or network hardware problems.

Another possible explanation is that you have a memory leak, and that the slowdown is caused by the GC taking more and more time as you run out of memory. This will show up in the GC logs if you have them enabled.

like image 146
Stephen C Avatar answered Oct 20 '22 03:10

Stephen C