圖片來源:Netty權威指南(第2版)
使用Netty進行客戶端開發主要有以下幾個步驟:
Bootstrap b = new Bootstrap();
Bootstrap是Socket客戶端創建工具類,通過API設置創建客戶端相關的參數,異步發起客戶端連接。
EventLoopGroup group = new NioEventLoopGroup();
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
此處的NioSocketChannel類似於Java NIO提供的SocketChannel。
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HelloClientHandler()); } });
用於調度和執行網絡事件。
// 發起異步連接操作 ChannelFuture f = b.connect(host, port).sync();
SocketChannel執行connect()操作後有以下三種結果:
需求:客戶端端實現,連接服務器端,並向服務器端發送hello Netty。(注:本代碼使用的netty是netty-all-5.0.0.Alpha1-sources.jar版本)
服務器端代碼見Netty學習之服務器端創建
客戶端代碼:
import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class HelloClient { public void connect(int port, String host) throws Exception { // 配置客戶端NIO線程組 EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HelloClientHandler()); } }); // 發起異步連接操作 ChannelFuture f = b.connect(host, port).sync(); // 等待客戶端鏈路關閉 f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; new HelloClient().connect(port, "127.0.0.1"); } }
import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; public class HelloClientHandler extends ChannelHandlerAdapter { private final ByteBuf message; public HelloClientHandler() { byte[] req="hello Netty".getBytes(); message=Unpooled.buffer(req.length); message.writeBytes(req); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(message); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }
程序運行結果:
1、Netty權威指南