java.net.SocketTimeoutException:读取超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2481660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.SocketTimeoutException: Read timed out
提问by Rafael Soto
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.
我有一个具有客户端服务器架构的应用程序。客户端使用 Java Web Start 和 Java Swing/AWT,sert 使用 HTTP 服务器/Servlet 和 Tomcat。通信是从对象的序列化开始的,创建一个 ObjectOutput 序列化一个字节数组并发送到服务器,分别称为 ObjectInputStream 和反序列化。
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.
应用程序遵循正确通信到某个并发时间,此时开始显示错误“SocketException 读取超时”。当服务器调用我的 servlet doPost 方法中的 ObjectInputStream.getObject() 方法时,就会发生错误。
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.
tomcat 会变慢并且错误开始减少服务器响应时间,直到我必须重新启动服务器并且一切正常后崩溃时间。
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
input.readObject() 是错误所在
回答by Stephen C
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.
- 未能设置 Content-length 标头(或将其设置为错误的值),
- 未能刷新输出流,和/或
- 不关闭插座的输出侧。
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.
另一种可能的解释是您有内存泄漏,并且速度变慢是由于内存不足时 GC 花费的时间越来越长。如果您启用了它们,这将显示在 GC 日志中。
回答by vetri
I think During high Concurrency, the Socket Timeout set in Tomcat is Expired and the connection is closed. The next read by Tomcat for that connection is greater than the server socket timeout specified in the server.
If you want to avoid this problem you have to increase the timeout on the server-side which is expired in your case. But not advisable.
BTW you did not give enough information. Did you increase the no of threads for connection in Tomcat? If you did, this surely would happen.
我认为在高并发期间,Tomcat中设置的Socket Timeout已过期并关闭连接。Tomcat 对该连接的下一次读取大于服务器中指定的服务器套接字超时。
如果你想避免这个问题,你必须增加服务器端的超时时间,这在你的情况下已经过期。但不可取。
顺便说一句,你没有提供足够的信息。您是否增加了Tomcat中用于连接的线程数?如果你这样做了,这肯定会发生。