Last time I made a server using Netty, but today I will continue to implement it on the client side as well.
➀ Processing such as access generation and termination
public class EchoClient {
private final String host;
private final int port;
public EchoClient() {
this(0);
}
public EchoClient(int port) {
this("localhost", port);
}
public EchoClient(String host, int port) {
this.host = host;
this.port = port;
}
public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(this.host, this.port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("connected...");
ch.pipeline().addLast(new EchoClientHandler());
}
});
System.out.println("created..");
ChannelFuture cf = b.connect().sync();
System.out.println("connected...");
cf.channel().closeFuture().sync();
System.out.println("closed..");
} finally {
group.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception {
new EchoClient("127.0.0.1", 31535).start();
}
}
② Function activated by timing (should be callback)
--channelActive () --When a connection to the server is created --channelRead0 () --When the server receives the data --exceptionCaught () --When a reller is detected
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client channelActive..");
ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("client channelRead..");
ByteBuf buf = msg.readBytes(msg.readableBytes());
System.out.println("Client received:" + ByteBufUtil.hexDump(buf) + "; The value is:" + buf.toString(Charset.forName("utf-8")));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
** Starting the server and waiting for a connection **
class EchoServer started and listen on /0:0:0:0:0:0:0:0:31535
** Start the server and make a request to the server ** ** The output of each client and server is as follows **
--Client
created..
connected...
client channelActive..
connected...
client channelRead..
Client received:4e6574747920726f636b7321; The value is:Netty rocks!
closed..
Process finished with exit code 0
--Server
class EchoServer started and listen on /0:0:0:0:0:0:0:0:31535
connected...; Client:/127.0.0.1:56967
server channelRead...; received:PooledUnsafeDirectByteBuf(ridx: 0, widx: 12, cap: 1024)
server channelReadComplete..
This time I tried to make a NIO transfer path using Netty, compared to ServerSocketChannel and SocketChannel, it became easier because there was no need to write a low-level API.
Recommended Posts