zoukankan      html  css  js  c++  java
  • Socket通讯-Netty框架实现Java通讯

    Netty简介

    Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
    
    也就是说,Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用。Netty相当简化和流线化了网络应用的编程开发过程,例如,TCP和UDP的socket服务开发。
    
    “快速”和“简单”并不用产生维护性或性能上的问题。Netty 是一个吸收了多种协议的实现经验,这些协议包括FTP,SMTP,HTTP,各种二进制,文本协议,并经过相当精心设计的项目,最终,Netty 成功的找到了一种方式,在保证易于开发的同时还保证了其应用的性能,稳定性和伸缩性。
    

    本文的目的

    使用Netty实现一个Socket通讯,包括客户端和服务端,通过服务端进行监听,客户端发送信息,服务端可进行接收,并进行返回数据,完成一个完整的通讯。
    

    工程结构

    这里写图片描述

    POM文件配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.taowd.socket</groupId>
        <artifactId>SocketDemo2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.6.Final</version>
            </dependency>
        </dependencies>
    </project>

    服务端代码

    EchoServer.java

    package Server;
    
    import java.nio.charset.Charset;
    
    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.bytes.ByteArrayEncoder;
    import io.netty.handler.codec.string.StringEncoder;
    
    public class EchoServer {
        private final int port;
    
        public EchoServer(int port) {
            this.port = port;
        }
    
        public void start() throws Exception {
            EventLoopGroup bossGroup = new NioEventLoopGroup();
    
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                ServerBootstrap sb = new ServerBootstrap();
                sb.option(ChannelOption.SO_BACKLOG, 1024);
                sb.group(group, bossGroup) // 绑定线程池
                        .channel(NioServerSocketChannel.class) // 指定使用的channel
                        .localAddress(this.port)// 绑定监听端口
                        .childHandler(new ChannelInitializer<SocketChannel>() { // 绑定客户端连接时候触发操作
    
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                System.out.println("报告");
                                System.out.println("信息:有一客户端链接到本服务端");
                                System.out.println("IP:" + ch.localAddress().getHostName());
                                System.out.println("Port:" + ch.localAddress().getPort());
                                System.out.println("报告完毕");
    
                                ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
                                ch.pipeline().addLast(new EchoServerHandler()); // 客户端触发操作
                                ch.pipeline().addLast(new ByteArrayEncoder());
                            }
                        });
                ChannelFuture cf = sb.bind().sync(); // 服务器异步创建绑定
                System.out.println(EchoServer.class + " 启动正在监听: " + cf.channel().localAddress());
                cf.channel().closeFuture().sync(); // 关闭服务器通道
            } finally {
                group.shutdownGracefully().sync(); // 释放线程池资源
                bossGroup.shutdownGracefully().sync();
            }
        }
    
        public static void main(String[] args) throws Exception {
    
            new EchoServer(8888).start(); // 启动
        }
    }
    

    EchoServerHandler.java

    package Server;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import io.netty.channel.ChannelFutureListener;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    
    public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    
        /*
         * channelAction
         *
         * channel 通道 action 活跃的
         *
         * 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据
         *
         */
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println(ctx.channel().localAddress().toString() + " 通道已激活!");
        }
    
        /*
         * channelInactive
         *
         * channel 通道 Inactive 不活跃的
         *
         * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
         *
         */
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            System.out.println(ctx.channel().localAddress().toString() + " 通道不活跃!");
            // 关闭流
    
        }
    
        /**
         * 
         * @author Taowd
         * TODO  此处用来处理收到的数据中含有中文的时  出现乱码的问题
         * 2017年8月31日 下午7:57:28
         * @param buf
         * @return
         */
        private String getMessage(ByteBuf buf) {
            byte[] con = new byte[buf.readableBytes()];
            buf.readBytes(con);
            try {
                return new String(con, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 功能:读取服务器发送过来的信息
         */
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            // 第一种:接收字符串时的处理
            ByteBuf buf = (ByteBuf) msg;
            String rev = getMessage(buf);
            System.out.println("客户端收到服务器数据:" + rev);
    
        }
    
        /**
         * 功能:读取完毕客户端发送过来的数据之后的操作
         */
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            System.out.println("服务端接收数据完毕..");
            // 第一种方法:写一个空的buf,并刷新写出区域。完成后关闭sock channel连接。
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            // ctx.flush();
            // ctx.flush(); //
            // 第二种方法:在client端关闭channel连接,这样的话,会触发两次channelReadComplete方法。
            // ctx.flush().close().sync(); // 第三种:改成这种写法也可以,但是这中写法,没有第一种方法的好。
        }
    
        /**
         * 功能:服务端发生异常的操作
         */
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
            System.out.println("异常信息:
    " + cause.getMessage());
        }
    }
    

    客户端代码

    EchoClient.java

    package Cilent;
    
    import java.net.InetSocketAddress;
    import java.nio.charset.Charset;
    
    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.bytes.ByteArrayEncoder;
    import io.netty.handler.codec.string.StringEncoder;
    import io.netty.handler.stream.ChunkedWriteHandler;
    
    public class EchoClient {
        private final String host;
        private final int port;
    
        public EchoClient() {
            this(0);
        }
    
        public EchoClient(int port) {
            this("localhost", port);
        }
    
        public EchoClient(String host, int port) {
            this.host = host;
            this.port = port;
        }
    
        public void start() throws Exception {
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                Bootstrap b = new Bootstrap();
                b.group(group) // 注册线程池
                        .channel(NioSocketChannel.class) // 使用NioSocketChannel来作为连接用的channel类
                        .remoteAddress(new InetSocketAddress(this.host, this.port)) // 绑定连接端口和host信息
                        .handler(new ChannelInitializer<SocketChannel>() { // 绑定连接初始化器
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                System.out.println("正在连接中...");
                                ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
                                ch.pipeline().addLast(new EchoClientHandler());
                                ch.pipeline().addLast(new ByteArrayEncoder());
                                ch.pipeline().addLast(new ChunkedWriteHandler());
    
                            }
                        });
                // System.out.println("服务端连接成功..");
    
                ChannelFuture cf = b.connect().sync(); // 异步连接服务器
                System.out.println("服务端连接成功..."); // 连接完成
    
                cf.channel().closeFuture().sync(); // 异步等待关闭连接channel
                System.out.println("连接已关闭.."); // 关闭完成
    
            } finally {
                group.shutdownGracefully().sync(); // 释放线程池资源
            }
        }
    
        public static void main(String[] args) throws Exception {
            new EchoClient("127.0.0.1", 8888).start(); // 连接127.0.0.1/65535,并启动
    
        }
    }

    EchoClientHandler.java

    package Cilent;
    
    import java.nio.charset.Charset;
    
    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.ByteBufUtil;
    import io.netty.buffer.Unpooled;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;
    import io.netty.util.CharsetUtil;
    
    public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    
        /**
         * 向服务端发送数据
         */
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("客户端与服务端通道-开启:" + ctx.channel().localAddress() + "channelActive");
    
            String sendInfo = "Hello 这里是客户端  你好啊!";
            System.out.println("客户端准备发送的数据包:" + sendInfo);
            ctx.writeAndFlush(Unpooled.copiedBuffer(sendInfo, CharsetUtil.UTF_8)); // 必须有flush
    
        }
    
        /**
         * channelInactive
         *
         * channel 通道 Inactive 不活跃的
         *
         * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
         *
         */
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("客户端与服务端通道-关闭:" + ctx.channel().localAddress() + "channelInactive");
        }
    
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
            System.out.println("读取客户端通道信息..");
            ByteBuf buf = msg.readBytes(msg.readableBytes());
            System.out.println(
                    "客户端接收到的服务端信息:" + ByteBufUtil.hexDump(buf) + "; 数据包为:" + buf.toString(Charset.forName("utf-8")));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
            System.out.println("异常退出:" + cause.getMessage());
        }
    }

    执行结果图

    客户端信息 
    服务端信息

  • 相关阅读:
    MySQL创建用户并修改权限
    Jenkins触发项目构建
    dotnet-cnblogs-tool使用与坑
    Jenkins集成Jmeter接口测试(Freestyle Project)
    Jenkins发送邮件没有解析变量
    JMeter + Maven in Jenkins
    Charles&Fiddler 对手机捉包失败原因分析
    Postman 脚本
    Selenium+Chrome浏览器自动加载Flash
    idea上进行远程调试项目步骤纪录
  • 原文地址:https://www.cnblogs.com/jtlgb/p/8757587.html
Copyright © 2011-2022 走看看