java timeout超时不抛异常,java.net.SocketTimeoutException:读取超时

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

解决方案

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.

`OkHttpUtil`是一个封装了OkHttp库功能的工具类,OkHttp是一个流行的网络请求库,用于在Android和Java应用程序中发起HTTP请求。当使用`OkHttpUtil`调用接口时,如果遇到`java.net.SocketTimeoutException: timeout`异常,这意味着在进行网络请求时,连接或读取操作超出了设定的超时时间,导致操作未能在指定的时间内完成。 `java.net.SocketTimeoutException`异常通常有以下几种情况: 1. 服务器没有在指定的超时时间内发送任何数据,包括响应头。 2. 在建立连接过程中,服务器没有在指定的超时时间内作出回应。 3. 读取操作(例如,从服务器读取响应)没有在指定的超时时间内完成。 在OkHttp中,可以通过设置请求的超时时间来避免此类异常。例如,可以在构建OkHttpClient时设置连接超时(connect timeout)、读取超时(read timeout)和写入超时(write timeout): ```java OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) // 设置连接超时时间为10秒 .readTimeout(30, TimeUnit.SECONDS) // 设置读取超时时间为30秒 .writeTimeout(10, TimeUnit.SECONDS) // 设置写入超时时间为10秒 .build(); ``` 如果遇到了`SocketTimeoutException`异常,可以通过以下方式处理: 1. 增加超时时间,以适应网络状况不佳或服务器响应慢的情况。 2. 实现重试机制,如果遇到超时异常,可以自动重新发起请求。 3. 对于请求结果进行监控,异常发生时可以进行相应的异常处理,比如提示用户网络状况不佳。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值