A simple Netty-EchoDemo

Posted by sametch on Sun, 18 Aug 2019 08:01:36 +0200

This blog Uncle Cat's Blog , reprint please state source

Read this about "4 minutes"

Readable Population: Java-Netty Junior

Echo Simple Communications Case

Version: netty 4.1. *

Statement: The purpose of this article is to re-share the discussion of official Netty related cases, add some personal understanding and point analysis.

This is InChat Cases from address , with detailed comments, it's easier to see than the official ones.

Official case address: https://netty.io/4.1/xref/io/...

text

  • EchoClient (Client)
  • EchoClientHandler
  • EchoServer (server)
  • EchoServerHandler

Key Points

  • SslContext

Official Introduction: https://netty.io/4.1/api/io/n...

Public Abstract class, Secure Socket Protocol implementation acts as factory SSLEngine and SlHandler.Internally, it implements SSL_CTX via JDK SSLContext or OpenSSL, and how to use it, if you need ssl encryption

  • SslContextBuilder

Official Introduction: https://netty.io/4.1/api/io/n...

The builder used to configure the new SslContext for creation, which contains several methods which are not supplemented here, so you can take a look

  • InsecureTrustManagerFactory

Official Introduction: https://netty.io/4.1/api/io/n...

Unsafe factors to trust all X.509 certificates without any validation of TrustManagerFactory

Note: Do not use TrustManagerFactory in production.It is purely for testing purposes and therefore very unsafe.

  • SelfSignedCertificate

Official Introduction: https://netty.io/4.1/api/io/n...

Generate temporary self-signed certificates for testing

Note: Do not use such generated certificates and private keys in production.It is purely for testing purposes and therefore very unsafe.It even generates faster internally using unsafe pseudo-random generators

Project Source

  • EchoClient
/**
 * @ClassName EchoClient
 * @Description An example of a simple reply communication
 * @Author MySelf
 * @Date 2019/8/17 17:56
 * @Version 1.0
 **/
public final class EchoClient {

    //Determine whether to encrypt
    static final boolean SSL = System.getProperty("ssl") != null;
    //Listen for local services
    static final String HOST = System.getProperty("host", "127.0.0.1");
    //Listening Port
    static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
    //Size of the message sent, for EchoClientHandler
    static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

    public static void main(String[] args) throws Exception {
        //Public Abstract class, Secure Socket Protocol implementation acts as factory SSLEngine and SlHandler.Internally, it implements SSL_CTX through JDK SSLContext or OpenSSL
        final SslContext sslCtx;
        if (SSL){
            //Builder for configuring a new SslContext for creation
            sslCtx = SslContextBuilder.forClient()
                    //Trusted Manager for Verifying Remote Endpoint Certificates
                    //InsecureTrustManagerFactory: Unsafe factor to trust all X.509 certificates without any validation of TrustManagerFactory
                    //Note: Do not use TrustManagerFactory in production.It is purely for testing purposes and therefore very unsafe.
                    .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        }else {
            sslCtx = null;
        }
        //event loop
        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 sc) throws Exception {
                            ChannelPipeline p = sc.pipeline();
                            //Understand the usage of SslContext
                            if (sslCtx != null){
                                p.addLast(sslCtx.newHandler(sc.alloc(),HOST,PORT));
                            }
                            p.addLast(new EchoClientHandler());
                        }
                    });
            //The code behind this sync will execute
            ChannelFuture f = b.connect(HOST,PORT).sync();
            System.out.println("before-----");

            //The code behind this sync will not execute
            f.channel().closeFuture().sync();
            System.out.println("after-----");
        }finally {
            group.shutdownGracefully();
        }
    }

}
  • EchoClientHandler
/**
 * @ClassName EchoClientHandler
 * @Description TODO
 * @Author MySelf
 * @Date 2019/8/17 18:06
 * @Version 1.0
 **/
public class EchoClientHandler extends ChannelInboundHandlerAdapter {

    private final ByteBuf firstMessage;

    public EchoClientHandler(){
        //Get SIZE E of EchoClient
        //Unpooled:ByteBuf creates a new one by allocating new space or by wrapping or copying existing byte arrays, byte buffers, and strings
        firstMessage = Unpooled.buffer(EchoClient.SIZE);
        for (int i = 0; i < firstMessage.capacity(); i++){
            firstMessage.writeByte((byte)i);
        }
    }

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

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
  • EchoServer
/**
 * @ClassName EchoServer
 * @Description Server
 * @Author MySelf
 * @Date 2019/8/17 18:15
 * @Version 1.0
 **/
public final class EchoServer {

    static final boolean SSL = System.getProperty("ssl") != null;
    static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));

    public static void main(String[] args) throws Exception {
        final SslContext sslCtx;
        if (SSL){
            //SelfSignedCertificate: Generate temporary self-signed certificates for testing
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        }else{
            sslCtx = null;
        }

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        final EchoServerHandler serverHandler = new EchoServerHandler();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,100)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            if (sslCtx != null){
                                p.addLast(sslCtx.newHandler(ch.alloc()));
                            }
                            p.addLast(serverHandler);
                        }
                    });

            ChannelFuture f = b.bind(PORT).sync();

            f.channel().closeFuture().sync();

        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

}
  • EchoServerHandler
/**
 * @ClassName EchoServerHandler
 * @Description TODO
 * @Author MySelf
 * @Date 2019/8/17 18:14
 * @Version 1.0
 **/
@ChannelHandler.Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

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

Public number: Java cat says

Learning and Communication Group: 728698035

Current architecture design (code farmer) and entrepreneurship technical consultant, unrestrained mediocrity, love open source, chat about program life and irregular dry goods.

Topics: Java Netty SSL socket