最近在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一直是在運行沒有被殺掉,但為什么會出現這么奇怪的事情,
連接上除錯線和沒連上具體有什么區別?
求大俠告知
uj5u.com熱心網友回復:
自己占1樓,先頂為敬uj5u.com熱心網友回復:
需要 做 Service 保活 處理 ; 前臺 和 可見 服務 一般不會被回收; 服務 如果 進入 后臺 , 就容易被殺死 ;結合 Service 保活, 實作 Socket 長連接 ;
給一個 Service 保活的 參考方案 : https://www.jianshu.com/p/22a708c74c1e
uj5u.com熱心網友回復:
樓主解決了嗎?我也出現這個問題不是保活的問題、我的Service是單起的一個行程、行程沒被殺死、所以不是Service的問題
uj5u.com熱心網友回復:
是哪一步呼叫出現java.io.IOException: Software caused connection abort例外?是連接時、發送資料時還是接收資料時?
uj5u.com熱心網友回復:
貼上堆疊來看一下uj5u.com熱心網友回復:
我也出現相同問題,報錯例外如下:
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)
求大佬指點
uj5u.com熱心網友回復:
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樓一樣,這個是說明在接受資料時出現的報錯對吧?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/9212.html
標籤:Android
