zbb20180930 java,nio,netty Netty5.0用法
Netty5.0用法
Maven坐標
<dependencies> <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version </dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling --> <dependency> <groupId>org.jboss.marshalling</groupId> <artifactId>jboss-marshalling <version>1.3.19.GA</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling-serial --> <dependency> <groupId>org.jboss.marshalling</groupId <artifactId>jboss-marshalling-serial</artifactId> <version>1.3.18.GA</version> <scope>test</scope> </dependency>
</dependencies> |
創建服務器端
class ServerHandler extends ChannelHandlerAdapter { /** * 當通道被調用,執行該方法 */ @Override publicvoid channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接收數據 String value = (String) msg; System.out.println("Server msg:" + value); // 回復給客戶端 “您好!” String res = "好的..."; ctx.writeAndFlush(Unpooled.copiedBuffer(res.getBytes())); }
}
publicclass NettyServer {
publicstaticvoid main(String[] args) throws InterruptedException { System.out.println("服務器端已經啟動...."); // 1.創建2個線程,一個負責接收客戶端連接, 一個負責進行 傳輸數據 NioEventLoopGroup pGroup = new NioEventLoopGroup(); NioEventLoopGroup cGroup = new NioEventLoopGroup(); // 2. 創建服務器輔助類 ServerBootstrap b = new ServerBootstrap(); b.group(pGroup, cGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024) // 3.設置緩沖區與發送區大小 .option(ChannelOption.SO_SNDBUF, 32 * 1024).option(ChannelOption.SO_RCVBUF, 32 * 1024) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protectedvoid initChannel(SocketChannel sc) throws Exception { sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ServerHandler()); } }); ChannelFuture cf = b.bind(8080).sync(); cf.channel().closeFuture().sync(); pGroup.shutdownGracefully(); cGroup.shutdownGracefully();
}
}
|
創建客戶端
class ClientHandler extends ChannelHandlerAdapter {
/** * 當通道被調用,執行該方法 */ @Override publicvoid channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接收數據 String value = (String) msg; System.out.println("client msg:" + value); }
}
publicclass NettyClient {
publicstaticvoid main(String[] args) throws InterruptedException { System.out.println("客戶端已經啟動...."); // 創建負責接收客戶端連接 NioEventLoopGroup pGroup = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(pGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protectedvoid initChannel(SocketChannel sc) throws Exception { sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ClientHandler()); } }); ChannelFuture cf = b.connect("127.0.0.1", 8080).sync(); cf.channel().writeAndFlush(Unpooled.wrappedBuffer("itmayiedu".getBytes())); cf.channel().writeAndFlush(Unpooled.wrappedBuffer("itmayiedu".getBytes())); // 等待客戶端端口號關閉 cf.channel().closeFuture().sync(); pGroup.shutdownGracefully();
}
} |
zbb20180930 java,nio,netty Netty5.0用法