/** * 这是server的基本格式。基本server都是这样,分四步 * 1.创建boss和work 线程 这个可以选。我选的是netty标志的支持nio异步的 * 2.ServerBootstrap group ,创建channel 这里可以选不同的协议的,我选的是tcp/ip协议的,这个可以选 * 3.加入ServerInitializer 这个是第二部分 * 4.最后监听 就行了 后面那一长串就是监听监听 */ public class HelloServer { public static void main(String[] args) { //epoll 是linux上支持的selector //EpollEventLoopGroup boss1 = new EpollEventLoopGroup(); EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup work = new NioEventLoopGroup(5); ServerBootstrap bootstrap = new ServerBootstrap(); try { bootstrap.group(boss1, work); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new HelloServerInitializer()); ChannelFuture f = bootstrap.bind(7894).sync().channel().closeFuture().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ boss.shutdownGracefully(); work.shutdownGracefully(); } } }
2.HelloServerInitializer
** * 这是一个中间量,可以相当于一个pipe 一个socket 过来 需要经过各种各种的handler 来编码解码 * 这就是定义那一个pipeline 然后随便加 后面的socketChannel 是一个TCP/IP socket。 */ public class HelloServerInitializer extends ChannelInitializer<SocketChannel>{ @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //pipeline.addLast("Sdecoder", new StringDecoder()); //pipeline.addLast("Sencoder",new StringEncoder()); //pipeline 这个方法可以加入很多编码,解码的的类,非常多,这里是对string 和对象的 pipeline.addLast(new StringEncoder()); pipeline.addLast(new StringDecoder()); pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null))); pipeline.addLast(new ObjectEncoder()); //最后加入自己定义的handler 可以加很多 pipeline.addLast("myHander",new HelloServerHandler()); } }
3.HelloServerHandler
public class HelloServerHandler extends ChannelInboundHandlerAdapter{ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("channel up"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub //读入一个msg System.out.println(msg); //写出去一个msg ctx.write(msg); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub ctx.flush(); }
客户端代码:
public class Client { public static void main(String[] args) { EventLoopGroup work = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); try { bootstrap.group(work).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // TODO Auto-generated method stub ch.pipeline().addLast(new ObjectEncoder(),new ObjectDecoder(ClassResolvers.cacheDisabled(null)),new HelloClientHandler()); } }); bootstrap.connect("127.0.0.1", 7894).sync().channel().closeFuture().sync(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
这是客户端的client
public class HelloClientHandler extends ChannelInboundHandlerAdapter{ public final List<String> list = new ArrayList<String>(); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub System.out.println("client channel up"); for(int i=0;i<10;i++){ list.add(String.valueOf(i)+","); } ctx.writeAndFlush(list); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println(msg); ctx.write(msg); }
相关推荐
Netty4.0全部jar包.开发时候只需要倒入总的哪一个netty4.0.jar就行了 后缀为resources.jar的全部是源码。 简单的代码例子在netty-example-resources.jar里面。
Netty (netty-netty-4.0.56.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
NULL 博文链接:https://bijian1013.iteye.com/blog/2340636
NIO socket开发,netty4.0工具包。
描述文档请看我的个人博客:www.mesoftware.cn
Springboot2.0.8集成 netty4 ,使用protobuf作为ping的数据交换,比json更加的小巧,占用数据量更小,可用于任何第三方应用做心跳监控。 已完成功能: - 客户端授权验证(基于protoBuff) - 心跳检测(基于protoBuff) ...
netty4.0 关于buffe
Netty4.0实现http服务,客户端通过http请求,服务器进行业务处理,返回响应,交互采用json格式
netty4.0.45jar包socket和http工具包,公司最近开发一个保险项目,用到socket,经过公司10个以上项目验证,该版本的netty4.0.45相当稳定,可以在生产环境上使用。
Netty4.0.54英文版API文档,与官网中文档内容一致,方便用户在离线环境下,开发Netty
Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序dsf。...netty, 4.0.28, Final, jar包, 含源码
包含javadoc,jar 两个文件夹。 doc中是文档资料。 jar是源码及常用例子,均好源码,好使。
netty4.0 各种应用类型的demo源码 比较全
Netty project - an event-driven asynchronous network application framework http://netty.io/
Netty官方4.0.42版jar包
NULL 博文链接:https://bijian1013.iteye.com/blog/2340639
NULL 博文链接:https://bijian1013.iteye.com/blog/2340634
NULL 博文链接:https://bijian1013.iteye.com/blog/2340638
NULL 博文链接:https://bijian1013.iteye.com/blog/2340632
netty 4.0版本