Netty network communication example four

Netty network communication example one: http://donald-draper.iteye.com/blog/2383326
netty network communication example two: http://donald-draper.iteye.com/blog/2383328
netty network communication example three: http: //donald-draper.iteye.com/blog/2383392In
the last article, we are going through an example to see an example of using encoder and decoder. The example is to provide the server with client computing requests and return the results to client. The codecs of the previous examples are all implemented by ourselves. Today, let's take a look at an example of Netty's textline-based codec communication:
server:
package netty.main.telnet;

import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import netty.initializer.telnet.TelnetServerInitializer;

/**
 * textline communication server
 * @author donald
 * June 30, 2017
 * 11:07:08 AM
 */
public final class TelnetServer {
	private static final Logger log = LoggerFactory.getLogger(TelnetServer.class);
	private static final boolean SSL = System.getProperty("ssl") != null;
    private static final  String ip = "192.168.31.153";
    private static final int port = Integer.parseInt(System.getProperty("port", SSL? "10010" : "10020"));
    public static void main(String[] args) throws Exception {
        // Configure SSL.
        final SslContext sslCtx;
        if (SSL) {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } else {
            sslCtx = null;
        }
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBoot = new ServerBootstrap();
            serverBoot.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new TelnetServerInitializer(sslCtx));
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip,port);
            ChannelFuture f = serverBoot.bind(inetSocketAddress).sync();
            log.info("=========Server is start=========");
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

Server processor Initializer:
package netty.initializer.telnet;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.ssl.SslContext;
import netty.handler.telnet.TelnetServerHandler;

/**
 * Creates a newly configured {@link ChannelPipeline} for a new channel.
 */
public class TelnetServerInitializer extends ChannelInitializer<SocketChannel> {
    private static final StringDecoder DECODER = new StringDecoder();
    private static final StringEncoder ENCODER = new StringEncoder();
    private static final TelnetServerHandler SERVER_HANDLER = new TelnetServerHandler();
    private final SslContext sslCtx;
    public TelnetServerInitializer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(ch.alloc()));
        }
        // Add the text line codec combination first,
        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        // the encoder and decoder are static as these are sharable
        pipeline.addLast(DECODER);
        pipeline.addLast(ENCODER);
        // and then business logic.
        pipeline.addLast(SERVER_HANDLER);
    }
}

Server handler:
package netty.handler.telnet;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.net.InetAddress;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author donald
 * June 30, 2017
 * 11:02:29 AM
 * Sharable indicates that this object is shared between channels
 */
@Sharable
public class TelnetServerHandler extends SimpleChannelInboundHandler<String> {
	private static final Logger log = LoggerFactory.getLogger(TelnetServerHandler.class);
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // Send greeting for a new connection.
        ctx.write("Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
        ctx.write("It is " + new Date() + " now.\r\n");
        ctx.flush();
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, String request) throws Exception {
        // Generate and write a response.
        String response;
        boolean close = false;
        log.info("========recieve message from client:"+request);
        if (request.isEmpty()) {
            response = "Please type something.\r\n";
        } else if ("bye".equals(request.toLowerCase())) {
            response = "Have a good day!\r\n";
            close = true;
        } else {
            response = "Did you say '" + request + "'?\r\n";
        }

        // We do not need to write a ChannelBuffer here.
        // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
        ChannelFuture future = ctx.write(response);

        // Close the connection after sending 'Have a good day!'
        // if the client has sent 'bye'.
        if (close) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }

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

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

Client:
package netty.main.telnet;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import netty.initializer.telnet.TelnetClientInitializer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * textline communication client
 * @author donald
 * June 30, 2017
 * 11:26:30 AM
 */
public final class TelnetClient {
	private static final Logger log = LoggerFactory.getLogger(TelnetServer.class);
    static final boolean SSL = System.getProperty("ssl") != null;
    public static final String ip = System.getProperty("host", "192.168.31.153");
    public static final int port = Integer.parseInt(System.getProperty("port", SSL? "10010" : "10020"));

    public static void main(String[] args) throws Exception {
        // Configure SSL.
        final SslContext sslCtx;
        if (SSL) {
            sslCtx = SslContextBuilder.forClient()
                .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            sslCtx = null;
        }

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new TelnetClientInitializer(sslCtx));
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip,port);
            // Start the connection attempt.
            Channel ch = b.connect(inetSocketAddress).sync().channel();
            log.info("=========Client is start=========");
            // Read commands from the stdin.
            ChannelFuture lastWriteFuture = null;
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for (;;) {
                String line = in.readLine();
                if (line == null) {
                    break;
                }
                // Sends the received line to the server.
                lastWriteFuture = ch.writeAndFlush(line + "\r\n");

                // If user typed the 'bye' command, wait until the server closes
                // the connection.
                if ("bye".equals(line.toLowerCase())) {
                    ch.closeFuture().sync();
                    break;
                }
            }
            // Wait until all messages are flushed before closing the channel.
            if (lastWriteFuture != null) {
                lastWriteFuture.sync();
            }
        } finally {
            group.shutdownGracefully();
        }
    }
}


Client processor Initializer:
package netty.initializer.telnet;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.ssl.SslContext;
import netty.handler.telnet.TelnetClientHandler;
import netty.main.telnet.TelnetClient;

/**
 * Creates a newly configured {@link ChannelPipeline} for a new channel.
 */
public class TelnetClientInitializer extends ChannelInitializer<SocketChannel> {
    private static final StringDecoder DECODER = new StringDecoder();
    private static final StringEncoder ENCODER = new StringEncoder();
    private static final TelnetClientHandler CLIENT_HANDLER = new TelnetClientHandler();
    private final SslContext sslCtx;
    public TelnetClientInitializer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }
    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();

        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(ch.alloc(), TelnetClient.ip, TelnetClient.port));
        }
        // Add the text line codec combination first,
        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast(DECODER);
        pipeline.addLast(ENCODER);

        // and then business logic.
        pipeline.addLast(CLIENT_HANDLER);
    }
}


Client processor:

package netty.handler.telnet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 *
 * @author donald
 * June 30, 2017
 * 10:59:47 AM
 * Sharable indicates that this object is shared between channels
 */
@Sharable
public class TelnetClientHandler extends SimpleChannelInboundHandler<String> {
    private static final Logger log = LoggerFactory.getLogger(TelnetClientHandler.class);
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    	log.info("====recieve message from server:"+msg);
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}


Start the server and client, enter hello server in the client console, enter bye in a newline,
server:
[INFO ] 2017-07-06 12:56:52 io.netty.handler.logging.LoggingHandler [id: 0x1ee25138] REGISTERED
[INFO] 2017-07-06 12:56:52 io.netty.handler.logging.LoggingHandler [id: 0x1ee25138] BIND: /192.168.31.153:10020
[INFO] 2017-07-06 12:56:52 netty .main.telnet.TelnetServer =========Server is start=========
[INFO] 2017-07-06 12:56:52 io.netty.handler.logging.LoggingHandler [ id: 0x1ee25138, L:/192.168.31.153:10020] ACTIVE
[INFO ] 2017-07-06 12:56:58 io.netty.handler.logging.LoggingHandler [id: 0x1ee25138, L:/192.168.31.153:10020] READ: [id: 0x4c1ab5f9, L:/192.168.31.153:10020 - R:/192.168.31.153:13357]
[INFO ] 2017-07-06 12:56:58 io.netty.handler.logging.LoggingHandler [id: 0x1ee25138, L:/192.168.31.153:10020] READ COMPLETE
[INFO ] 2017-07-06 12:57:13 netty.handler.telnet.TelnetServerHandler ========recieve message from client:hello server
[INFO ] 2017-07-06 12:57:19 netty.handler.telnet.TelnetServerHandler ========recieve message from client:yes
[INFO ] 2017-07-06 12:57:22 netty.handler.telnet.TelnetServerHandler ========recieve message from client:bye
客户端:
[INFO ] 2017-07-06 12:56:58 netty.main.telnet.TelnetServer =========Client is start=========
[INFO ] 2017-07-06 12:56:59 netty.handler.telnet.TelnetClientHandler ====recieve message from server:Welcome to donaldHP!
[INFO ] 2017-07-06 12:56:59 netty.handler.telnet.TelnetClientHandler ====recieve message from server:It is Thu Jul 06 12:56:59 CST 2017 now.
hello server
[INFO ] 2017-07-06 12:57:13 netty.handler.telnet.TelnetClientHandler ====recieve message from server:Did you say 'hello server'?
yes
[INFO ] 2017-07-06 12:57:19 netty.handler.telnet.TelnetClientHandler ====recieve message from server:Did you say 'yes'?
bye
[INFO ] 2017-07-06 12:57:22 netty.handler.telnet.TelnetClientHandler ====recieve message from server:Have a good day!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326370051&siteId=291194637