- 浏览: 238844 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
maotou1988:
android基于netty的发送图片与文字demo,楼主共勉 ...
在android中使用netty框架通信 -
ZSRTFAT:
学习了,谢谢分享
android 基于surfaceview 的多点触控实现 -
charlotte:
学习了!!!!!
Java nio 客户端连接Server -
nilo:
您好, 本人菜鸟, 正在学习设计模式. 当我学习完简单工厂模式 ...
策略模式 -
fokman:
一江春水邀明月 写道缺少SocketInfoUtils Th ...
Java nio 客户端连接Server
今日将android里面使用的socket 方式改成Oio,因为之前使用的是Netty框架,所以直接将Nio替换成Oio,然后修改了一些其他的代码。
为了维护的方便我定义了一个BaseClient,用来处理停止socket线程。
public abstract class BaseClient { protected Channel m_channel; protected String m_tag; public BaseClient(String tag) { m_tag = tag; } public Channel getChannel() { return m_channel; } public void stop(boolean releaseResOnly) { this.stopInThread(releaseResOnly); } protected void createBootstrap() { System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv6Addresses", "false"); } protected void stopInThread(Bootstrap bootstrap, boolean releaseResOnly) { try { final Bootstrap bs = bootstrap; final Channel ch = m_channel; final boolean resOnly = releaseResOnly; m_channel = null; Thread t = new Thread(new Runnable() { public void run() { try { if (!resOnly) { if (ch != null) { ch.getCloseFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture cf) throws Exception { if (bs != null) { final Bootstrap bs2 = bs; new Thread(new Runnable() { public void run() { try { System.out.println(m_tag + "--- netty ch.close and releaseExtraRes-1"); bs2.releaseExternalResources(); System.out.println(m_tag + "--- netty ch.close and releaseExtraRes-1 done"); } catch (Throwable th) { } } }).start(); } } }); ch.close(); } } else { if (bs != null) { try { System.out.println(m_tag + "--- netty releaseExtraRes-2"); bs.releaseExternalResources(); System.out.println(m_tag + "--- netty releaseExtraRes-2 done"); } catch (Throwable th) { } } } } catch (Exception ee) { ee.printStackTrace(); } } }); t.start(); } catch (Exception e) { e.printStackTrace(); } }//stopInThread() protected abstract void stopInThread(boolean releaseResOnly);
CxClient继承了BaseClient,增加了一些关于不同的错误编码,方便在日志中查看
public class CxClient extends BaseClient { private ClientBootstrap m_bootstrap; private CxListener m_listener; private final Timer timer; public CxClient(String tag) { super(tag); timer = new HashedWheelTimer(); } @Override protected void createBootstrap() { super.createBootstrap(); m_bootstrap = new ClientBootstrap(new OioClientSocketChannelFactory(Executors.newCachedThreadPool())); final CxClient client = this; m_bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pip = Channels.pipeline(); pip.addLast("timeout", new ReadTimeoutHandler(timer, 300)); pip.addLast("decoder", new CxDecoder()); pip.addLast("handler", new CxHandler(m_listener, client)); return pip; } }); m_bootstrap.setOption("tcpNoDelay", true); m_bootstrap.setOption("keepAlive", true); m_bootstrap.setOption("reuseAddress", true); m_bootstrap.setOption("connectTimeoutMillis", "7000"); } public void start(String host, int port, CxListener li) { try { m_listener = li; createBootstrap(); System.out.println("CxClient::start() Connecting... " + host + ":" + port); ChannelFuture f = m_bootstrap.connect(new InetSocketAddress(host, port)); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture cf) throws Exception { if (m_listener != null) { if (cf.isSuccess()) { m_channel = cf.getChannel(); m_listener.connected(); } else { Throwable th = cf.getCause(); System.out.println("CxClient::start() A 1"); int errorCode = CxnetConstants.UNKNOWN_EXCEPTION; if (th != null) { Class<?> c = th.getClass(); if (c == java.net.BindException.class) errorCode = CxnetConstants.BIND_EXCEPTION; else if (c == java.net.ConnectException.class) errorCode = CxnetConstants.CONNECT_EXCEPTION; else if (c == java.net.MalformedURLException.class) errorCode = CxnetConstants.MAILFORMEDURL_EXCEPTION; else if (c == java.net.NoRouteToHostException.class) errorCode = CxnetConstants.NOROUTETOHOST_EXCEPTION; else if (c == java.net.PortUnreachableException.class) errorCode = CxnetConstants.PORTUNREACHABLE_EXCEPTION; else if (c == java.net.ProtocolException.class) errorCode = CxnetConstants.PROTOCOL_EXCEPTION; else if (c == java.net.SocketException.class) errorCode = CxnetConstants.SOCKET_EXCEPTION; else if (c == java.net.SocketTimeoutException.class) errorCode = CxnetConstants.SOCKETTIMEOUT_EXCEPTION; else if (c == java.net.UnknownHostException.class) errorCode = CxnetConstants.UNKNOWNHOST_EXCEPTION; else if (c == java.net.UnknownServiceException.class) errorCode = CxnetConstants.UNKNOWNSERVICE_EXCEPTION; else if (c == java.net.URISyntaxException.class) errorCode = CxnetConstants.URISYNTAX_EXCEPTION; th.printStackTrace(); System.out.println("CxClient::start() A 2 errCode=" + errorCode); } m_listener.disconnected(errorCode); } } } }); } catch (Exception e) { System.out.println("CxClient::start() excep B 1"); e.printStackTrace(); System.out.println("CxClient::start() excep B 2"); m_listener.disconnected(CxnetConstants.UNKNOWN_EXCEPTION); } }//start() @Override protected void stopInThread(boolean releaseResOnly) { final Bootstrap bs = m_bootstrap; m_bootstrap = null; super.stopInThread(bs, releaseResOnly); } }//end cls - CxClient
发表评论
-
获得手机相关信息的实现方法
2015-04-04 00:55 774本文介绍了如何通过android提供的servi ... -
android判断当前sdk版本以及是否是平板
2013-11-19 13:53 1278最近做了一个项目中需要使用appWidget,但是 ... -
android 程序检查当前应用是否在运行
2012-11-09 17:05 1065最近做一个项目,需要知道当前的android 程序是 ... -
netty 找不到org.jboss.netty.bootstrap.ClientBootstrap解决
2012-09-25 14:20 4018今天碰到一个莫名其妙的问题,在做socket连接的时候发 ... -
定义文本样式
2012-09-06 14:19 1061android程序中的按钮经常需要定义文字的大小,是否加粗,居 ... -
点击按钮显示不同的状态
2012-09-06 14:05 979android给我们提供的UI还比较丰富,但是基本上都 ... -
android 两个listView 共存的显示问题
2012-08-07 09:55 2486如果在一个layout里面需要定义有两个listvie ... -
定义带箭头的Button 样式
2012-08-02 10:58 11152定义一个圆角的Button 样式 <style ... -
android UI入门三 自定义背景样式
2012-08-01 15:23 1370在android 开发应用中,大部份的开发人员 ... -
android UI入门二 定义图片横竖屏切换正确显示
2012-07-30 13:20 3206android开发的时候通常会使用到横竖屏切换,但是 ... -
android UI入门一
2012-07-24 12:20 915在ios系统中,常常看到绚丽的画面让人心动不已。然而在 ... -
android spinner 实现国码选择
2012-07-13 15:33 3926android 中得Spinner相当于是一个下拉框 ... -
在自己的android应用中调用打电话实现
2012-07-13 15:10 1049最近在做一个项目,需要在自己写的应用中调用打电话给别人 ... -
android 定义圆角Button
2012-06-30 15:31 5028定义styles.xml <?xml version= ... -
android 自定义导航控件
2012-06-26 15:19 7825在编写android应用程序的过程中,经常会使用到导航 ... -
android observer 处理handler消息
2012-06-12 09:41 2049在开发android网络应用程序,通常都会使用han ... -
android wifi 处理
2012-06-12 09:35 3184在android应用程序中可以很方便的使用wifi来处理不同的 ... -
android canvas 使用
2012-04-06 09:38 7630Canvas 又称为画布,在游戏开发过程中是一个经常使用的类。 ... -
android 基于surfaceview 的多点触控实现
2012-04-06 08:55 12370hello,各位朋友。最近接到一个功能需要实现多 ... -
给android imageView(图片) 添加超链接
2012-03-29 09:29 8048hi ,各位好朋友,最近忙于开发没有时间更新博客。 有很多朋 ...
相关推荐
从最简单的Socked编程,到目前为止已经有了开源的框架,那就是Netty,它Jobss开发的一个网络异步应用框架,能高快捷的实现网络客户端和服务器端的优秀框架,而本实例就是基于这个框架实现的聊天程序,希望对你有用
使用netty 4.x 编写的聊天室程序,实现client-client client-server server-client 全双工通信
本压缩包实现了一个简单的Netty应答程序,客户端发送消息,服务器端回复响应消息
基于netty框架编写的socket服务器
Java进阶技术-netty进阶之路
Netty进阶之路,跟着案例学Netty,李林峰大神新作。值得一读。
《Netty进阶之路:跟着案例学Netty》中的案例涵盖了Netty的启动和停止、内存、并发多线程、性能、可靠性、安全等方面,囊括了Netty绝大多数常用的功能及容易让人犯错的地方。在案例的分析过程中,还穿插讲解了Netty...
Netty进阶之路 跟着案例学Netty 整本书无密码,Netty进阶之路 跟着案例学Netty
netty socketio 在线聊天程序 源码可以运行。可以用来开发需客服模块,在线聊天室,广告推送等功能
这是Netty与ActiveMQ的程序代码范例,用Netty处理网络讯息的接收,用ActiveMQ处理异步的处理。
《Netty进阶之路 跟着案例学Netty》_李林锋_2018-11-01
《Netty权威指南》是异步非阻塞通信领域的经典之作,基于最新版本Netty 5.0编写,是国内首本深入介绍Netty原理和架构的技术书籍,也是作者多年实战经验的总结和浓缩。在理论方面,讲解了Netty的逻辑架构模型和核心...
精选自1000多个一线业务实际案例,从原理到实践全景式讲解Netty项目实践,快速领悟Netty专家花大量时间积累的经验,提高编程水平及分析解决问题的能力,《Netty木又威指南》作者力作,众专家力荐 Netty将Java NIO...
NULL 博文链接:https://bijian1013.iteye.com/blog/2340977
netty4.x的hello world 范例,可以运行,直接导入工程,需要依赖包netty-all-4.0.25.final.jar,可以去MAVEN仓库下载或者百度下载。
Netty (netty-netty-4.0.56.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
实现微信QQ等IM的简单文本聊天功能,服务端模式。
java netty接收串口数据 开启windows串口工具 发送串口数据调试助手