socket编程 error:Error:read ECONNRESET.怎么回事

4281人阅读
Connection reset by peer.
Operation not supported.
Protocol family not supported.
Address family not supported by protocol family.
Address already in use.
Cannot assign requested address.
Network is down.
No buffer space available.
Socket is already connected.
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:119710次
积分:1949
积分:1949
排名:第10238名
原创:62篇
转载:62篇
评论:15条
(2)(2)(1)(1)(1)(1)(2)(2)(2)(3)(1)(9)(5)(13)(5)(4)(4)(7)(31)(4)(5)(3)(2)(3)(11)21606人阅读
package com.ucaimalls.
import java.io.ByteArrayOutputS
import java.io.InputS
import java.net.HttpURLC
import java.net.URL;
import org.apache.http.HttpR
import org.apache.http.client.methods.HttpG
import org.apache.http.impl.client.DefaultHttpC
import org.apache.http.util.EntityU
import android.util.L
* 网络连接类
public class HttpHelper
public static final int TIME_OUT_MILL = 10000;// 超时毫秒数
* HttpURLConnection 请求方式,直接返回字符串数据
public static String getHttpUrlConnData(String url) throws Exception
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestProperty(&accept&, &*/*&);
conn.setRequestProperty(&connection&, &Keep-Alive&);
conn.setRequestProperty(&user-agent&,
&Mozilla/4.0 ( MSIE 6.0; Windows NT 5.1; SV1)&);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(TIME_OUT_MILL);
conn.setReadTimeout(TIME_OUT_MILL);
InputStream is = conn.getInputStream();
ByteArrayOutputStream dis = new ByteArrayOutputStream();
int realRead = 0;
byte[] buff = new byte[1024];
while ((realRead = is.read(buff )) != -1)
dis.write(buff,0,realRead);
String data = new String(dis.toByteArray(), &UTF-8&).trim();
// System.out.println(data);
if (dis != null)
dis.close();
if(conn!=null)
conn.disconnect();//断开连接---问题产生的原因了
今天遇到了这么个错误:
09-11 10:02:34.849: W/System.err(17963): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-11 10:02:34.849: W/System.err(17963): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
09-11 10:02:34.849: W/System.err(17963): at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
09-11 10:02:34.859: W/System.err(17963): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
09-11 10:02:34.859: W/System.err(17963): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
09-11 10:02:34.859: W/System.err(17963): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-11 10:02:34.859: W/System.err(17963): at java.io.InputStream.read(InputStream.java:163)
09-11 10:02:34.869: W/System.err(17963): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
09-11 10:02:34.869: W/System.err(17963): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
09-11 10:02:34.869: W/System.err(17963): at libcore.io.Streams.readAsciiLine(Streams.java:201)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
09-11 10:02:34.869: W/System.err(17963): at com.ucaimalls.util.HttpHelper.getHttpUrlConnData(HttpHelper.java:37)
09-11 10:02:34.869: W/System.err(17963): at com.ucaimalls.util.QueryLocationTask.doInBackground(QueryLocationTask.java:49)
09-11 10:02:34.869: W/System.err(17963): at com.ucaimalls.util.QueryLocationTask.doInBackground(QueryLocationTask.java:1)
09-11 10:02:34.869: W/System.err(17963): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-11 10:02:34.869: W/System.err(17963): at java.lang.Thread.run(Thread.java:856)
09-11 10:02:34.879: W/System.err(17963): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.Posix.recvfromBytes(Native Method)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.Posix.recvfrom(Posix.java:131)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
09-11 10:02:34.889: W/System.err(17963): ... 20 more
在网上找了下答案(老外的回答 啊)
ok, the answer was that it's the server's fault - it had to close the connection after each request .
it might be that android keeps a pool of connections and use the old one or something like that .
anyway , now it works.
EDIT: according to the API of&HttpURLConnection&, this can be solved on the client side too:
The input and output streams returned by this class are not buffered. Most callers should wrap the returned streams with BufferedInputStream or BufferedOutputStream. Callers that do only bulk reads or writes may omit buffering. When transferring large amounts
of data to or from a server, use streams to limit how much data is in memory at once. Unless you need the entire body to be in memory at once, process it as a stream (rather than storing the complete body as a single byte array or string).
To reduce latency, this class may reuse the same underlying Socket for multiple request/response pairs. As a result, HTTP connections may be held open longer than necessary. Calls to disconnect() may return the socket to a pool of connected sockets. This behavior
can be disabled by setting the http.keepAlive system property to false before issuing any HTTP requests. The http.maxConnections property may be used to control how many idle connections to each server will be held.
taken from: http///reference/java/net/HttpURLConnection.html
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:117927次
积分:1666
积分:1666
排名:第12471名
原创:48篇
转载:39篇
评论:22条
(2)(3)(1)(3)(6)(1)(5)(2)(5)(11)(1)(6)(3)(1)(7)(7)(18)(5)

我要回帖

更多关于 socket编程 的文章

 

随机推荐