java.net.SocketException异常:recvfrom的失败:ECONNRESET(连接被对方复位) [英] java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
问题描述
我在Android的一个HTTP服务器。我为每一个HTTP请求的一个新的线程中提到的链接:
I have a HTTP server in Android. I create a new thread for each of the HTTP requests as in the link mentioned:
<一个href=\"http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java\" rel=\"nofollow\">http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java.
当我发出多个GET请求,有时我越来越喜欢以下异常:
When I issue multiple GET requests, sometimes I am getting an exception like the following:
01-22 10:28:22.779: W/System.err(2019): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
01-22 10:28:22.779: W/System.err(2019): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
01-22 10:28:22.779: W/System.err(2019): at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
01-22 10:28:22.779: W/System.err(2019): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
01-22 10:28:22.779: W/System.err(2019): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
01-22 10:28:22.784: W/System.err(2019): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.HttpRequestParser.parseHead(HttpRequestParser.java:71)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.impl.AbstractHttpServerConnection.receiveRequestHeader(AbstractHttpServerConnection.java:141)
01-22 10:28:22.784: W/System.err(2019): at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:135)
01-22 10:28:22.784: W/System.err(2019): at com.example.devicecommunication.ConnectService$WorkerThread.run(ConnectService.java:744)
01-22 10:28:22.784: W/System.err(2019): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
01-22 10:28:22.784: W/System.err(2019): at libcore.io.Posix.recvfromBytes(Native Method)
01-22 10:28:22.784: W/System.err(2019): at libcore.io.Posix.recvfrom(Posix.java:131)
01-22 10:28:22.784: W/System.err(2019): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
01-22 10:28:22.789: W/System.err(2019): at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
01-22 10:28:22.789: W/System.err(2019): ... 10 more
我不知道为什么发生这种情况。下面是使用code和的handleRequest()方法或conn.close()。
I am not sure why this occurs. Below is the code used and this exception occurs at handleRequest() method or in conn.close().
class WorkerThread extends Thread{
HttpService httpService;
HttpServerConnection conn;
public WorkerThread(HttpService httpService, HttpServerConnection conn){
super();
this.httpService = httpService;
this.conn = conn;
}
public void run(){
HttpContext context = new BasicHttpContext(null);
try {
Log.d(TAG,"Going to call Handle request here");
this.httpService.handleRequest(this.conn, context);
} catch (ConnectionClosedException ex) {
ex.printStackTrace();
Log.d(TAG,"Client closed connection exception");
} catch (IOException ex) {
ex.printStackTrace();
Log.d(TAG,"I/O exceptionnnnn " + ex.getMessage());
} catch (HttpException ex) {
Log.d(TAG,"Unrecoverable HTTP protocol violation: " + ex.getMessage());
}
finally {
Log.d(TAG,"Inside Finally Block");
try {
this.conn.close();
Log.d(TAG,"Connection closed successfully");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
任何想法,为什么发生这种情况或如何解套?
Any idea why this occurs or how can it be resolved?
推荐答案
当我用下面的code片段通过DefaultHttpServerConnection属性为handle()方法,我没有得到这个异常:
When I passed the DefaultHttpServerConnection attribute to the handle() method using the following code snippet, I did not get this exception:
在的WorkerThread类,使用下面的code传递DefaultHttpServerConnection连接对象:
In the WorkerThread class, pass the DefaultHttpServerConnection connection object using the following code:
HttpContext context = new BasicHttpContext(null);
context.setAttribute("XXX",this.conn);
在手柄()方法,用得到的连接对象,
In the handle() method, get the connection object using,
(DefaultHttpServerConnection)context.getAttribute("XXX");
希望它可以帮助别人!
Hope it helps someone!
这篇关于java.net.SocketException异常:recvfrom的失败:ECONNRESET(连接被对方复位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!