Netty 之 Socket通信

1.导入pom依赖

  <dependencies>

    ......

    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.39.Final</version>
    </dependency>
  </dependencies>

2.服务端

  a.创建服务端启动类

public class StartServer {

    //端口号
    private int port;

    public StartServer(int port) {
        this.port = port;
    }

    //启动方法
    public void start() throws Exception {
        //负责接收客户端的连接的线程。线程数设置为1即可,netty处理链接事件默认为单线程,过度设置反而浪费cpu资源
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        //负责处理数据传输的工作线程。线程数默认为CPU核心数乘以2
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            //在ServerChannelInitializer中初始化ChannelPipeline责任链,并添加到serverBootstrap中
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel channel) {
                    //添加编解码
                    channel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
                    channel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
                    channel.pipeline().addLast("socketHandler", new SocketHandler());
                }
            });
            //标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            //是否启用心跳保活机制
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);

            //绑定端口后,开启监听
            ChannelFuture future = bootstrap.bind(port).sync();
            //等待服务监听端口关闭
            future.channel().closeFuture().sync();
        } finally {
            //释放资源
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    //测试代码
    public static void main(String[] args) {
        try {
            int port = 8080;
            new StartServer(port).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

  b.创建Socket处理器

public class SocketHandler extends SimpleChannelInboundHandler<String> {

    /**
     * 客户端发消息会触发
     */
    @Override
    public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("[" + this.getIP(ctx) + "]收到消息:" + msg);
        ClientManager.getInstance().handleMsg(this.getIP(ctx), "This is response");
    }

    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //添加channel信息
        ClientManager.getInstance().putChannel(this.getIP(ctx), ctx.channel());
        System.out.println("[" + this.getIP(ctx) + "]已连接。。。");
    }

    /**
     * 客户端断开连接会触发
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //删除失效的channel
        ClientManager.getInstance().removeChannel(getIP(ctx));
        ctx.close();
        System.out.println("[" + this.getIP(ctx) + "]已断开。。。");
    }

    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable t) throws Exception {
        System.out.println("[" + this.getIP(ctx) + "]发生异常:" + t);
        ctx.close();
    }

    /**
     * 获取IP地址
     */
    private String getIP(ChannelHandlerContext ctx) {
        String socketString = ctx.channel().remoteAddress().toString();
        int index = socketString.indexOf(":");
        String ip = socketString.substring(1, index);
        return ip;
    }

}

  c.创建客户端管理类

public class ClientManager {

    private static ClientManager instance = new ClientManager();
    private ClientManager(){}
    public static ClientManager getInstance(){
        return instance;
    }

    //IP与信道的对应关系
    private Map<String, Channel> channelMap = new ConcurrentHashMap<>();

    //添加信道
    public void putChannel(String ip, Channel channel){
        this.channelMap.put(ip, channel);
    }

    //删除信道
    public void removeChannel(String ip){
        this.channelMap.remove(ip);
    }

    //发送消息
    public void sendMsg(String ip, String msg){
        Channel channel = this.channelMap.get(ip);
        if(channel != null){
            channel.writeAndFlush(msg);
        }
    }

    //处理消息
    public void handleMsg(String ip, String msg){
        this.sendMsg(ip, msg);
    }

}

3.客户端

  a.创建客户端启动类

public class StartClient {

    //主机名/IP
    private String host;
    //端口号
    private int port;

    public StartClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    //启动方法
    public void start() throws Exception {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    channel.pipeline().addLast("decoder", new StringDecoder());
                    channel.pipeline().addLast("encoder", new StringEncoder());
                    channel.pipeline().addLast(new SimpleChannelInboundHandler<String>(){
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
                            System.out.println("收到响应:" + msg);
                        }
                    });
                }
            });

            //建立连接
            ChannelFuture future = bootstrap.connect(host, port).sync();
            //发送消息
            future.channel().writeAndFlush("This is Request");
            //等待服务监听端口关闭
            future.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

    //测试代码
    public static void main(String[] args) {
        try {
            String host = "127.0.0.1";
            int port = 8080;
            new StartClient(host, port).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

4.参考文章

  https://www.cnblogs.com/happy2010/p/10895209.html

  https://www.jianshu.com/p/b60180a0a0e6

猜你喜欢

转载自www.cnblogs.com/vettel0329/p/12206456.html