Netty Authoritative Guide Reading Notes (4)

netty server code
public class MyServer {
public void bind(int port) {
EventLoopGroup ioGroup = new NioEventLoopGroup(); // Receive the connection initiated by the client to the server
EventLoopGroup workerGroup = new NioEventLoopGroup();// Receive SocketChannel network read and write
try {
ServerBootStrap b = new ServerBootStrap();
b.group(ioGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() { //Internal initialization Class implementation
@Override
private void initChannel(SocketChannel sc) throws Exception {
         sc.pipeline().addLast(new LineBasedFrameDecoder(1024));
sc.pipeline().addLast(new StringDecoder());
sc.pipeline().addLast(new ChanelHandlerAdapter() { // Internal class of server core processor
@Override
public void channelRead(...){...} // Core method, process the read information
public void channelReadComplete(...){...} // Write the message sent to the buffer array by the write method into the SocketChannel, the write method in channelRead only writes the message to the buffer array
public void exceptionCaught(... ){...}
Explanation: LineBasedFrameDecoder traverses the readable bytes in ByteBuf at a time to determine whether there is \n or \r\n. If there is, use this position as the end position, and end the bytes of the position interval from the scale index It forms a line. It is a decoder with a newline as the end mark.
It also supports configuring the maximum length of a single line. If the maximum length is continuously read and no newline is found, a TooLongFrameException will be thrown, and the previous The read exception code stream (to prevent memory overflow caused by the lack of separators in the exception code stream)
StringDecoder has a simple function, just converts the received object into a string.

netty client code

public class MyClientHandler {
public void connect(int port, String host) {
EventLoopGroup group = new NioEventLoopGroup();
try {
BootStrap b = new BootStrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>(){// Initialize
@Override
private void initChannel (SocketChannel sc) throws Exception {

sc.pipeline().addLast(new ChanelHandlerAdapter() { // Client core handler inner class
@Override
public void channelRead(...){...} // Core method, processing The read information
public void channelActive(...){...} // After the TCP link is successfully established, the NIO thread of Netty will call this method, which will call the flush method to send the request message to the server (Unlike the server, the server will call flush in ChannelReadComplete, and write the message sent to the buffer array through the write method into the SocketChannel
public void exceptionCaught(...){...}
})

From: http:/ /blog.csdn.net/xxcupid/article/details/50492204

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327064104&siteId=291194637