前言
参考Netty官网文档的例子,加了编码器、解码器。
实现了简单的tcp通讯。
代码很简单,最后我们会用Linux下的Telnet命令连接到服务端。并且给服务端发送消息。
代码
tcp服务端
package netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import sun.rmi.runtime.Log;
import java.util.logging.Logger;
public class DiscardServer {
private static final Logger LOG = Logger.getLogger(DiscardServerHandler.class.getName());