求救,android netty的问题 java.io.IOException: Software caused connection abort

a1111111984 2018-04-26 11:40:28
最近在android上做的一个简单的netty客户端,基本上是按照官网上的例子来做的
http://netty.io/4.1/xref/io/netty/example/securechat/package-summary.html

ChatClient:
final SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new SecureChatClientInitializer(sslCtx,user_code));

bootstrap.option(ChannelOption.SO_KEEPALIVE,true);
bootstrap.option(ChannelOption.TCP_NODELAY,true);
bootstrap.option(ChannelOption.SO_TIMEOUT, 5000);

cf = bootstrap.connect(new InetSocketAddress(HOST, PORT));

SecureChatClientInitializer:
pipeline.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));

// On top of the SSL handler, add the text line codec.
pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast("ping", new IdleStateHandler(5, 5, 5 * 3, TimeUnit.SECONDS));

// and then business logic.
pipeline.addLast(new SecureChatClientHandler(usercode));

SecureChatClientHandler:

public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.err.println(msg);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}

netty客户端写在service里面,诡异的是app页面保持活动的时候接收和发送消息都正常,用usb线连在电脑调试的时候也正常,一旦拔掉调试线,并把APP放在后台运行的时候,过大概40s左右的时间服务器就会出现连接丢失的情况,重新连上后可以重连,并报这样的错误 java.io.IOException: Software caused connection abort

可以确定的是service一直是在运行没有被杀掉,但为什么会出现这么奇怪的事情,

连接上调试线和没连上具体有什么区别?
求大侠告知

...全文
2526 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_40661559 2020-09-02
  • 打赏
  • 举报
回复
引用 11 楼 jzp12 的回复:
是哪一步调用出现java.io.IOException: Software caused connection abort异常?
是连接时、发送数据时还是接收数据时?


引用 12 楼 jzp12 的回复:
贴上堆栈来看一下


java.io.IOException: Software caused connection abort
at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:197)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:421)
at org.apache.mina.transport.socket.nio.NioProcessor.read(NioProcessor.java:317)
at org.apache.mina.transport.socket.nio.NioProcessor.read(NioProcessor.java:45)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:683)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:659)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:648)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.access$600(AbstractPollingIoProcessor.java:68)
at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1120)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)

我的跟13楼一样,这个是说明在接受数据时出现的报错对吧?
黎鹤 2020-04-02
  • 打赏
  • 举报
回复
我也出现相同问题,报错异常如下:

E/ServerHandler: Error handling request
    java.io.IOException: Software caused connection abort
        at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
        at sun.nio.ch.IOUtil.read(IOUtil.java:197)
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:421)
        at io.netty.buffer.UnpooledHeapByteBuf.setBytes(UnpooledHeapByteBuf.java:291)
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1140)
        at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:347)
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:697)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:632)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:549)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:511)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:784)
求大佬指点
jzp12 2020-03-23
  • 打赏
  • 举报
回复
贴上堆栈来看一下
jzp12 2020-03-23
  • 打赏
  • 举报
回复
是哪一步调用出现java.io.IOException: Software caused connection abort异常? 是连接时、发送数据时还是接收数据时?
acaixuediannao 2020-03-21
  • 打赏
  • 举报
回复
楼主解决了吗?我也出现这个问题 不是保活的问题、我的Service是单起的一个进程、进程没被杀死、所以不是Service的问题
韩曙亮 2019-03-28
  • 打赏
  • 举报
回复
需要 做 Service 保活 处理 ; 前台 和 可见 服务 一般不会被回收; 服务 如果 进入 后台 , 就容易被杀死 ;
结合 Service 保活, 实现 Socket 长连接 ;

给一个 Service 保活的 参考方案 : https://www.jianshu.com/p/22a708c74c1e
a1111111984 2018-04-26
  • 打赏
  • 举报
回复
自己占1楼,先顶为敬

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧