最近在android上做的一个简单的netty客户端,基本上是按照官网上的例子来做的
http://netty.io/4.1/xref/io/netty/example/securechat/package-summary.htmlChatClient:
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一直是在运行没有被杀掉,但为什么会出现这么奇怪的事情,连接上调试线和没连上具体有什么区别?
求大侠告知
http://netty.io/4.1/xref/io/netty/example/securechat/package-summary.htmlChatClient:
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一直是在运行没有被杀掉,但为什么会出现这么奇怪的事情,连接上调试线和没连上具体有什么区别?
求大侠告知
解决方案 »
- 如何指定安卓虚拟机新建avd的位置?
- Arduino新手请教问题
- OpenglES画立方体的时候的单位问题
- 求教:Activity为什么会自动调用finish()方法
- android 给 edittext 加 onkeylistener 删除键就没有效果了
- 对战游戏网络支持Android SDK 1.0开发者文档
- 问个jni相关的问题
- 新浪微博api里要用到curl 有人懂吗
- webView直接加载带中文的页面html,出现乱码?
- 求帮忙看这个日历空间怎么错了,大牛何在!!!!!
- 求助:关于android atudio的avd manager
- 请问哪位可以编写采集和显示类似ECG的app?新型应用,有重赏
结合 Service 保活, 实现 Socket 长连接 ; 给一个 Service 保活的 参考方案 : https://www.jianshu.com/p/22a708c74c1e