可能是java.net.SocketException的原因:recvfrom失败:ETIMEDOUT?

时间:2012-11-30 10:46:27

标签: java android

我的应用中遇到此问题。这很难发现。这是一个堆栈:

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$000(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$1.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

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

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);
        } 

您对此问题的原因有什么想法吗?

2 个答案:

答案 0 :(得分:5)

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

问候。

答案 1 :(得分:0)

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

  • 应该回复的一方意外关闭连接。

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