Netty 客户端

8次阅读

共计 1527 个字符,预计需要花费 4 分钟才能阅读完成。

示例代码
public class TimeClient {

public void connect(int port, String host) throws Exception {
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
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeClientHandler());
}
});

ChannelFuture f = b.connect(host, port).sync();

f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}

}

private class TimeClientHandler extends ChannelHandlerAdapter {

private ByteBuf firstMessage = null;

public TimeClientHandler() {
byte[] req = “66666”.getBytes();
firstMessage = Unpooled.buffer(req.length);
firstMessage.writeBytes(req);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(firstMessage);
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req, “UTF-8”);
System.out.println(body);
}
}
}
首先创建客户端处理 IO 读写的 NioEventLoopGroup 线程组, 然后继续创建客户端辅助启动类 Bootstrap, 随后需要对其进行配置.
与服务端不同的是, 它的 Channel 需要设置为 NioSocketChannel, 然后为其添加 Handler. 此处为了简单直接创建匿名内部类, 实现 initChannel 方法, 其作用是当创建 NioSocketChannel 成功之后, 在进行初始化时, 将它的 ChannelHandler 设置到 ChannelPipeline 中, 用于处理网络 IO 事件.
当客户端和服务端 TCP 链路建立成功之后, Netty 的 NIO 线程会调用 channelActive 方法, 发送查询时间的指令给服务端.
当服务端返回应答消息时, channelRead 方法被调用. 发生异常时, 调用 exceptionCaught 方法.

正文完
 0