java.net.SocketException: recvfrom failed: ETIMEDOUT 的原因可能是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13643652/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:29:19  来源:igfitidea点击:

What can be a cause of java.net.SocketException: recvfrom failed: ETIMEDOUT?

javaandroid

提问by Winte Winte

I have got this issue in my app. It is rare and difficult to spot. This is a stack:

我在我的应用程序中遇到了这个问题。这是罕见且难以发现的。这是一个堆栈:

2012-11-30 08:42:22.745myapp.package.MyCommand is failed.java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access
private static final AndroidHttpClient client = AndroidHttpClient.newInstance(TAG);

HttpUriRequest request = getRequest(context);
        HttpResponse response = client.execute(request);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK == statusCode) {
            final String json = EntityUtils.toString(response.getEntity());
            handleResult(json);
            Log.i(TAG, json);
        } else {
            handleError(SERVER_CODE);
        } 
0(PlainSocketImpl.java:46) at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191) at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82) at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174) at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180) at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235) at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259) at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279) at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:252) at myapp.package.HttpCommand.executeRequest(HttpCommand.java:176) at myapp.package.HttpCommand.execute(HttpCommand.java:83) at myapp.package.NetworkService.run(NetworkService.java:60) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out) at libcore.io.Posix.recvfromBytes(Native Method) at libcore.io.Posix.recvfrom(Posix.java:131) at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164) at libcore.io.IoBridge.recvfrom(IoBridge.java:503) ... 26 more

In my app I have a class that executes all command to the server:

在我的应用程序中,我有一个向服务器执行所有命令的类:

##代码##

Do you have any ideas what is the cause of this issue?

你知道这个问题的原因是什么吗?

回答by Luis

When receiving a "Connection Timeout" error, I would look for:

当收到“连接超时”错误时,我会寻找:

Regards.

问候。

回答by Horacio75

For me this error happened when:

对我来说,这个错误发生在:

  • the party which is supposed to reply closes the connection unexpectedly.
  • 应该回复的一方意外关闭了连接。

My situation: Android connecting to microprocessor via plain socket on port 8080. The problem was the uC would close the connection before actually sending the "OK" reply. Sending is apparently a deferred operation in this uC (ESP8266). The solution was to comment out the line closing the connection (This line of code was there from a previous version in which no reply was intended). Rgds.

我的情况:Android 通过端口 8080 上的普通套接字连接到微处理器。问题是 uC 会在实际发送“OK”回复之前关闭连接。在这个 uC (ESP8266) 中,发送显然是一个延迟操作。解决方案是注释掉关闭连接的行(这行代码来自以前没有回复的版本)。Rgds。