SocketException:recvfrom failed:发生ECONNRESET(由对等方重置连接)

时间:2013-09-24 14:18:13

标签: c# android wcf

我写了一个wcf REST服务(c#)和一个客户端(android)。我尝试通过webservice从mysql表中获取数据,并希望在Android设备上的ListView中显示它们。当我调试服务时,我可以看到,数据是在通用List中读取和写入的。

但是在方法结束时,当List返回时,客户端站点上会发生以下错误:

  

java.net.SocketException:recvfrom failed:ECONNRESET(由对等方重置连接)

这是服务接口的代码:

[OperationContract]
[WebGet(UriTemplate = "/{keyword}", ResponseFormat = WebMessageFormat.Json)]
List<object> GetEntity(string keyword);

这是我的服务方法的代码:

public List<object>GetEntity(string keyword)
    {
       conn.Open();
        if (keyword.ToUpper().Equals("USER"))
        {
            List<User> list = new List<User>();


            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            string sql = string.Format("SELECT * FROM " + USERDB);
            command.CommandText = sql;
            reader = command.ExecuteReader();
            String userName = "userName", userPassword = "userPassword", userId = "userId";

            while (reader.Read())
            {
                User user = new User(reader[userId].ToString(), reader[userName].ToString(), reader[userPassword].ToString());
                list.Add(user);
            }
            conn.Close();
            return list;
        }
        else if(keyword.ToUpper().Equals("BOOK"))
        {
             List<Book> list = new List<Book>();
             // some code...
        }
        conn.Close();
        return list;
 }

android eclipse中的堆栈跟踪:

09-25 18:14:51.614: W/System.err(10696): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-25 18:14:51.614: W/System.err(10696):    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
09-25 18:14:51.614: W/System.err(10696):    at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
09-25 18:14:51.614: W/System.err(10696):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
09-25 18:14:51.614: W/System.err(10696):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
09-25 18:14:51.614: W/System.err(10696):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-25 18:14:51.624: W/System.err(10696):    at sample.ShowSamples$AsyncSample.doInBackground(ShowSamples.java:105)
09-25 18:14:51.624: W/System.err(10696):    at sample.ShowSamples$AsyncSample.doInBackground(ShowSamples.java:1)
09-25 18:14:51.624: W/System.err(10696):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-25 18:14:51.624: W/System.err(10696):    at java.lang.Thread.run(Thread.java:856)
09-25 18:14:51.624: W/System.err(10696): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.Posix.recvfromBytes(Native Method)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.Posix.recvfrom(Posix.java:131)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
09-25 18:14:51.634: W/System.err(10696):    ... 24 more

感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:1)

问题仅存在于android Jelly Bean中,

这是我的解决方案

在连接catch子句中,

catch(IOException e){if(e.getMessage()。indexOf(“peer by peer”)&gt; 0)&lt;&lt;&lt;&lt;连接增益&gt;&gt;&gt ;;