Netty实例-简单的服务端-client实现,凝视具体
生活随笔
收集整理的這篇文章主要介紹了
Netty实例-简单的服务端-client实现,凝视具体
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ?書籍推薦: ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 實例代碼 :?http://download.csdn.net/detail/jiangtao_st/7677503
Netty Server端實現
/*** * <p>* Netty Server Simple* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/public class NettyServer {private final int port = 8989;@Testpublic void nettyServer(){EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());//綁定端口、同步等待ChannelFuture futrue = serverBootstrap.bind(port).sync();//等待服務監聽端口關閉futrue.channel().closeFuture().sync();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//退出,釋放線程等相關資源bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new SimpleServerHandler());}}}
Netty Client 實現
/*** * <p>* NettyClient 實現* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/ public class NettyClient {public void connect(int port,String host){EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new SimpleClientHandler());}});//發起異步鏈接操作ChannelFuture channelFuture = bootstrap.connect(host, port).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//關閉,釋放線程資源group.shutdownGracefully();}}@Testpublic void nettyClient(){new NettyClient().connect(8989, "localhost");}}
ServerHander 處理程序?
/*** * <p>* Server接收消息處理Handler* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/ public class SimpleServerHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf)msg;byte [] req = new byte[buf.readableBytes()];buf.readBytes(req);String message = new String(req,"UTF-8");System.out.println("Netty-Server:Receive Message,"+ message);} }
ClientHander 處理程序
/*** * <p>* Client Handler* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/ public class SimpleClientHandler extends ChannelInboundHandlerAdapter {private ByteBuf clientMessage;public SimpleClientHandler() {byte [] req = "Call-User-Service".getBytes();clientMessage = Unpooled.buffer(req.length);clientMessage.writeBytes(req);}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush(clientMessage);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf)msg;byte [] req = new byte[buf.readableBytes()];buf.readBytes(req);String message = new String(req,"UTF-8");System.out.println("Netty-Client:Receive Message,"+ message);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();} }
/*** * <p>* Netty Server Simple* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/public class NettyServer {private final int port = 8989;@Testpublic void nettyServer(){EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());//綁定端口、同步等待ChannelFuture futrue = serverBootstrap.bind(port).sync();//等待服務監聽端口關閉futrue.channel().closeFuture().sync();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//退出,釋放線程等相關資源bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new SimpleServerHandler());}}}
/*** * <p>* NettyClient 實現* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/ public class NettyClient {public void connect(int port,String host){EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new SimpleClientHandler());}});//發起異步鏈接操作ChannelFuture channelFuture = bootstrap.connect(host, port).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//關閉,釋放線程資源group.shutdownGracefully();}}@Testpublic void nettyClient(){new NettyClient().connect(8989, "localhost");}}
/*** * <p>* Server接收消息處理Handler* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/ public class SimpleServerHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf)msg;byte [] req = new byte[buf.readableBytes()];buf.readBytes(req);String message = new String(req,"UTF-8");System.out.println("Netty-Server:Receive Message,"+ message);} }
/*** * <p>* Client Handler* </p>* * @author 卓軒* @創建時間:2014年7月7日* @version: V1.0*/ public class SimpleClientHandler extends ChannelInboundHandlerAdapter {private ByteBuf clientMessage;public SimpleClientHandler() {byte [] req = "Call-User-Service".getBytes();clientMessage = Unpooled.buffer(req.length);clientMessage.writeBytes(req);}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush(clientMessage);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf)msg;byte [] req = new byte[buf.readableBytes()];buf.readBytes(req);String message = new String(req,"UTF-8");System.out.println("Netty-Client:Receive Message,"+ message);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();} }
總結
以上是生活随笔為你收集整理的Netty实例-简单的服务端-client实现,凝视具体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到蝙蝠好不好
- 下一篇: 研究笔记:iOS中使用WebViewPr