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)。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值