Netty5 简单栗子
来源:互联网 发布:安全大数据 编辑:程序博客网 时间:2024/05/02 00:12
Netty5 简单栗子
mark一个Netty5 简化版的栗子~,比较简单不用多说,直接上代码…
Server:
public class Server { public void init() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChildChannelHandler()); ChannelFuture channelFuture = bootstrap.bind(9999).sync(); System.out.println("NettyServer start up."); channelFuture.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } private class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new ServerHandler()); } } public static void main(String[] args) throws Exception { Server server = new Server(); server.init(); }}
ServerHandler:
public class ServerHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); String recmMsg = new String(bytes, "UTF-8"); System.out.println("Server receive : " + recmMsg); String resp = "This msg is from server: has Receive msg!"; ctx.write(Unpooled.copiedBuffer(resp.getBytes())); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); }}
Client:
public class Client { public void connect() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new ClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9999).sync(); channelFuture.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { Client client = new Client(); client.connect(); }}
ClientHandler:
public class ClientHandler extends ChannelHandlerAdapter { private final ByteBuf msgBuf; public ClientHandler() { byte[] bytes = "Hi, this msg is from client.".getBytes(); this.msgBuf = Unpooled.buffer(bytes.length); this.msgBuf.writeBytes(bytes); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(msgBuf); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); String respMsg = new String(bytes, "UTF-8"); System.out.println("Client receive : " + respMsg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println("Exception occurred!"); }}
0 0
- Netty5 简单栗子
- spring计时器简单栗子
- Netty5.0使用简单介绍
- Netty5基础知识介绍及简单使用
- 栗子
- Android自定义控件(含简单栗子)
- 栗子—EditText简单实现输入搜索
- 一颗简单的JDBC栗子
- 学习Java注解及简单应用栗子
- Tensorflow模拟简单线性模型小栗子
- Netty5 最简单的例子 Writing a Discard Server
- 3个netty5的例子,简单介绍netty的用法
- Netty5的例子,简单介绍Netty的用法
- 3个netty5的例子,简单介绍netty的用法
- Python用treeview模拟表格的简单栗子
- php 一些实用简单栗子一 汉子转拼音
- Netty5初探
- netty5简介
- 产品经理武林大会2016(深圳站)
- redis 集群官方中文文档翻译
- iOS App上架流程(2016)
- 死锁产生的必要条件及其处理办法
- Datatables的特性
- Netty5 简单栗子
- IP头、TCP头、UDP头详解以及定义
- ovs-of实现简单的桥
- Android(四)android学习书籍介绍+工具使用介绍
- JS表格组件神器bootstrap table详解
- 利用pandas对数据进行基本清洗
- 互联网协议入门
- 多线程编程指南摘录三:Run Loops(2)
- 进制转换