简体   繁体   English

Java套接字读取超时,错误后无法读取

[英]Java Socket Read Timeout, unable to read after the error

I am working on a fix to a Java code, which looks like this - 我正在修复Java代码,看起来像这样-

    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(POST);
    connection.setRequestProperty("Content-Type", CONTENT_TYPE);
    connection.setReadTimeout(5 * 60 * 1000);
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(parameters);
    wr.flush();
    wr.close();

    in = connection.getInputStream();
    connection = null;

It works fine on small amount of data exchange (including concurrent requests). 它适用于少量数据交换(包括并发请求)。 In cases where the data is large or when there are concurrent requests (for large datas), I get a "java.net.SocketTimeoutException: Read timed out" error. 如果数据很大或存在并发请求(对于大数据),则会收到“ java.net.SocketTimeoutException:读取超时”错误。 After this error, the program fails to read additional data or cater to additional requests. 出现此错误后,程序将无法读取其他数据或无法满足其他请求。 I have to restart the server (JBoss). 我必须重新启动服务器(JBoss)。

My initial idea was to increase the setReadTimeOut to 15. Doesn't help, still get the error for concurrent requests for large dataset. 我最初的想法是将setReadTimeOut增加到15。这无济于事,对于大型数据集的并发请求仍然会出错。

What are my other options? 我还有其他选择吗? How would I recover from a read timeout error? 如何从读取超时错误中恢复?

The above code is using java.net.*. 上面的代码使用的是java.net。*。

Appreciate any help..WM 感谢任何帮助..WM

I'm pretty sure it has to do with the DataOutputStream you are using to write the body of the http request. 我很确定这与您用来编写http请求的正文的DataOutputStream有关。 I've never seen anyone doing this with a DataOutputStream. 我从未见过有人使用DataOutputStream进行此操作。

A DataOutputStream is designed to write primitive java types (int, byte, long, etc) to an OutputStream. DataOutputStream旨在将原始Java类型(int,byte,long等)写入OutputStream。 When fed with a String, like you are doing, it will take each character (2 bytes), and convert it to a byte, while ignoring the highest byte of the character. 像您所做的那样,当给它喂字符串时,它将占用每个字符(2个字节),并将其转换为一个字节,同时忽略该字符的最高字节。

This might work for some characters that don't have their highest 8 bits set, but will surely lead to lost data sooner or later. 对于某些未设置最高8位字符的字符,这可能会起作用,但肯定会迟早导致数据丢失。 This could then lead to a http request being sent that specifies a content-length that is different from the length of the actual message body, which will in turn lead to the remote server waiting for more data that will never be received. 然后,这可能导致发送的http请求指定的内容长度与实际消息正文的长度不同,这反过来将导致远程服务器等待更多永远不会收到的数据。

I would replace the DataOutputStream with eg a BufferedWriter, which allows you to write character data. 我将用例如BufferedWriter替换DataOutputStream,它允许您写入字符数据。 Also consider using apache's HttpClient, which is far more sophisticated that java's HttpUrlConnection. 还可以考虑使用apache的HttpClient,它比Java的HttpUrlConnection更复杂。

Cheers, Tom 干杯汤姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM