This chapter provides the following functions: After a successful connection, the client sends data to the server. After receiving the data, the server returns a piece of data to the client.

The client sends data to the server

As mentioned earlier, the business logic is handled in a handler, so let’s add a handler

.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ch.pipeline().addLast(new FirstClientHandler()); }});Copy the code
The business logic that sends data to the server
public class FirstClientHandler extends ChannelInboundHandlerAdapter { @Override public void ChannelActive (ChannelHandlerContext CTX) {system.out.println (" Client starts writing out data "); ByteBuf buffer = getByteBuf(CTX); // 2. Send data ctx.channel().writeandFlush (buffer); } private ByteBuf getByteBuf(ChannelHandlerContext CTX) {// Byte data byte[] bytes = GetBytes (charset.forname (" utF-8 ")); // Apply a data structure to store information ByteBuf buffer = ctx.alloc().buffer(); // Put the information into the data structure buffer.writebytes (bytes); return buffer; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf byteBuf = (ByteBuf) msg; Println (" client reads data: "+ bytebuf.toString (charset.forname (" utF-8 "))); }}Copy the code

Description:

  • 1.ChannelInboundHandlerAdapterIs to implement theio.netty.channel.ChannelHandlerOf this interface, all that implement that interfacehandlerAll have the following life cycles:
state describe
ChannelUnregistered A channel has been created, but is not yet registered with EventLoop
ChannelRegistered The channel has been registered with the EventLoop
ChannelActive The channel is connected to the remote node and can now receive and send data
ChannelInactive The channel is not connected to the remote node
  • 2. ThehandlerinheritanceChannelInboundHandlerAdapterThis handler also has its attached methods, wherechannelReadRefers to a method called when data is read from a channel.

The server reads the client data

As with the client, add a handler to handle the business logic

.childHandler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel ch) { ch.pipeline().addLast(new FirstServerHandler()); }});Copy the code
The server accepts client data and the business logic that returns it to the client
public class FirstServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf byteBuf = (ByteBuf) msg; System.out.println(" server reads data :" + bytebuf.toString (charset.forname (" utF-8 "))); // Return data to client system.out.println (" server starts to write data "); ByteBuf out = getByteBuf(CTX); // Send data ctx.channel().writeAndFlush(out); } private ByteBuf getByteBuf(ChannelHandlerContext ctx) { byte[] bytes = GetBytes (charset.forname (" utF-8 ")); // Apply a data structure to store information ByteBuf buffer = ctx.alloc().buffer(); // Put the information into the data structure buffer.writebytes (bytes); return buffer; }}Copy the code

The resources

  • Netty Introduction and Actual Combat: Imitation writing wechat IM Instant Messaging System

    Booklet address: juejin.cn/book/684473…

    Github address: link.juejin. Im /? Target = HTTP…

  • Netty in Action

This article is written and licensed BY CC BY 3.0CN. Can be reproduced freely, quote, but the author must be signed and indicate where the article originated. If reprinted to wechat official account, please add the author’s official qr code at the end of the article.