java.net.SocketException: Connection reset by peer

09-04 00:18:36.660: E/NetTcpFileSendThread(1728): null
09-04 00:18:36.660: E/NetTcpFileSendThread(1728): java.net.SocketException: Connection reset by peer
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at org.apache.harmony.luni.platform.OSNetworkSystem.writeDirect(Native Method)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at dalvik.system.BlockGuard$WrappedNetworkSystem.writeDirect(BlockGuard.java:290)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at org.apache.harmony.nio.internal.SocketChannelImpl.writeImpl(SocketChannelImpl.java:454)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at org.apache.harmony.nio.internal.SocketChannelImpl.write(SocketChannelImpl.java:402)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at com.ccf.feige.net.NetTcpFileSendThread.sendFile(NetTcpFileSendThread.java:246)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at com.ccf.feige.net.NetTcpFileSendThread.SocketListening(NetTcpFileSendThread.java:147)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at com.ccf.feige.net.NetTcpFileSendThread.run(NetTcpFileSendThread.java:72)
09-04 00:18:36.660: E/NetTcpFileSendThread(1728):     at java.lang.Thread.run(Thread.java:1019)
09-04 00:18:36.660: W/System.err(1728): java.net.SocketException: Connection reset by peer
09-04 00:18:36.660: W/System.err(1728):     at org.apache.harmony.luni.platform.OSNetworkSystem.writeDirect(Native Method)
09-04 00:18:36.660: W/System.err(1728):     at dalvik.system.BlockGuard$WrappedNetworkSystem.writeDirect(BlockGuard.java:290)
09-04 00:18:36.670: W/System.err(1728):     at org.apache.harmony.nio.internal.SocketChannelImpl.writeImpl(SocketChannelImpl.java:454)
09-04 00:18:36.670: W/System.err(1728):     at org.apache.harmony.nio.internal.SocketChannelImpl.write(SocketChannelImpl.java:402)
09-04 00:18:36.670: W/System.err(1728):     at com.ccf.feige.net.NetTcpFileSendThread.sendFile(NetTcpFileSendThread.java:246)
09-04 00:18:36.670: W/System.err(1728):     at com.ccf.feige.net.NetTcpFileSendThread.SocketListening(NetTcpFileSendThread.java:147)
09-04 00:18:36.670: W/System.err(1728):     at com.ccf.feige.net.NetTcpFileSendThread.run(NetTcpFileSendThread.java:72)
09-04 00:18:36.670: W/System.err(1728):     at java.lang.Thread.run(Thread.java:1019)

问题:

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 .

可能是Android系统保留了一个老的连接池,然后使用了老的连接,导致失效。

 

 

解决方法:我是利用Nio来进行数据传输的,在Nio中,Accepted、Writable、Readable在同一个线程中,每次连接的时候就创建一个新的线程。但是没有注意到及时销毁老的进程,这样当老的连接线程因为监听而阻塞的时候,新的线程也在监听阻塞。两个线程可能公用一个SocketChannel。这样当回收老的进程的时候,新的进程的SocketChannel也被回收了。

导致连接重置( Connection reset by peer)。

以上是我的猜想。当我及时干掉老线程的时候,发现这个错误就没有了。

posted @ 2012-09-04 03:00  陈哈哈  阅读(5519)  评论(1编辑  收藏  举报