【Netty整理01-快速入门】Netty简单使用Demo(已验证)

多处摘抄或手打,为了十积分厚着脸皮标为原创,惭愧惭愧~本篇文章用于快速入门搭建一个简单的netty

应用,如想稍微深入系统的了解,请参照本人下一篇博客,链接:

 

参考地址:

官方文档:http://netty.io/wiki/user-guide-for-4.x.html

文案部分:

百度百科:https://baike.baidu.com/item/Netty/10061624?fr=aladdin

NIO:https://blog.csdn.net/skiof007/article/details/52873421

代码部分

https://blog.csdn.net/hangge110/article/details/51613988

更多参考:

各个对象以及常量的作用对照源码:https://blog.csdn.net/CoffeeAndIce/article/details/78987542

源代码学习1:https://www.cnblogs.com/katsura/p/5991428.html

源代码学习2:https://www.cnblogs.com/stevenczp/p/7581940.html

demo02:https://blog.csdn.net/Howinfun/article/details/81283721

 

Netty

 

    Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。

也就是说,Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用。Netty相当于简化和流线化了网络应用的编程开发过程,例如:基于TCP和UDP的socket服务开发。

 

NIO基本概念:点击打开链接

IO的方式通常分为几种,同步阻塞的BIO、同步非阻塞的NIO、异步非阻塞的AIO......

 

DEMO(手打https://blog.csdn.net/hangge110/article/details/51613988):

1、服务端

package com.zx.test;

import com.lk.netty.mulchat.dome.ServerIniterHandler;
import com.lk.netty.mulchat.dome.ServerMain;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.omg.CORBA.PRIVATE_MEMBER;

/**
 * 服务端生产者对象
 * This is an netty Server
 * @author  ZX
 * @date  2018年6月7日
 * Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输,
 * 例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象。
 * 深入学习:
 * https://www.cnblogs.com/katsura/p/5991428.html
 * https://www.cnblogs.com/stevenczp/p/7581940.html
 */
public class ServerTest {
    /**
     * 服务端口
     */
    private int port=9999;
    /**
     * 开启服务的方法
     */
    public void StartNetty(){

        /**创建两个EventLoop的组,EventLoop 这个相当于一个处理线程,
         是Netty接收请求和处理IO请求的线程。不理解的话可以百度NIO图解*/
        /* 
        相关资料:NioEventLoopGroup是一个处理I/O操作的多线程事件循环。
        Netty为不同类型的传输提供了各种EventLoopGroup实现。
        在本例中,我们正在实现一个服务器端应用程序,因此将使用两个NioEventLoopGroup。
        第一个,通常称为“boss”,接受传入的连接。
        第二个,通常称为“worker”,当boss接受连接并注册被接受的连接到worker时,处理被接受连接的流量。
        使用了多少线程以及如何将它们映射到创建的通道取决于EventLoopGroup实现,甚至可以通过构造函数进行配置。
        */
        EventLoopGroup acceptor = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            //1、创建启动类
            ServerBootstrap bootstrap = new ServerBootstrap();
            //2、配置启动参数等
            /**设置循环线程组,前者用于处理客户端连接事件,后者用于处理网络IO(server使用两个参数这个)
             *public ServerBootstrap group(EventLoopGroup group)
             *public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
             */
            bootstrap.group(acceptor,worker);
            /**设置选项
            * 参数:Socket的标准参数(key,value),可自行百度
            * eg:
            * bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            *bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            * */
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);

            //用于构造socketchannel工厂
            bootstrap.channel(NioServerSocketChannel.class);
            /**
             * 传入自定义客户端Handle(服务端在这里搞事情)
             */
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    // 注册handler
                    ch.pipeline().addLast(new SimpleServerHandler());
                }
            });

            // 绑定端口,开始接收进来的连接
            ChannelFuture f = bootstrap.bind(port).sync();


            // 等待服务器 socket 关闭 。
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            acceptor.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }



    public static void main(String[] args) {
        new ServerTest().StartNetty();
    }
}

 

 

2、服务端处理类

 

 

package com.zx.test;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SimpleServerHandler extends ChannelInboundHandlerAdapter {
    /**
     * 本方法用于读取客户端发送的信息
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("SimpleServerHandler.channelRead");
        ByteBuf result = (ByteBuf) msg;
        byte[] result1 = new byte[result.readableBytes()];
        // msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中
        result.readBytes(result1);
        String resultStr = new String(result1);
        // 接收并打印客户端的信息
        System.out.println("Client said:" + resultStr);
        // 释放资源,这行很关键
        result.release();

        // 向客户端发送消息
        String response = "hello client!";
        // 在当前场景下,发送的数据必须转换成ByteBuf数组
        ByteBuf encoded = ctx.alloc().buffer(4 * response.length());
        encoded.writeBytes(response.getBytes());
        ctx.write(encoded);
        ctx.flush();
    }

    /**
     * 本方法用作处理异常
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }

    /**
     * 信息获取完毕后操作
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

}

 

3、客户端

 

package com.zx.test;
import io.netty.bootstrap.Bootstrap;
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.channel.socket.nio.NioSocketChannel;

/**
 * 客户端消费者对象
 * This is an netty Client
 * @author ZX
 * @date   2018年6月7日
 */
public class ClientTest {
    public void connect(String host, int port) throws Exception {
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            /**
             *EventLoop的组
             */
            b.group(worker);
            /**
             * 用于构造socketchannel工厂
             */
            b.channel(NioSocketChannel.class);
            /**设置选项
             * 参数:Socket的标准参数(key,value),可自行百度
               保持呼吸,不要断气!
             * */
            b.option(ChannelOption.SO_KEEPALIVE, true);
            /**
             * 自定义客户端Handle(客户端在这里搞事情)
             */
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new SimpleClientHandler());
                }
            });
            /** 开启客户端监听*/
            ChannelFuture f = b.connect(host, port).sync();
            /**等待数据直到客户端关闭*/
            f.channel().closeFuture().sync();
        } finally {
            worker.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        ClientTest client=new ClientTest();
        client.connect("127.0.0.1", 9999);

    }
}

 

4、客户端处理类

 

package com.zx.test;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
    /**
     * 本方法用于接收服务端发送过来的消息
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("SimpleClientHandler.channelRead");
        ByteBuf result = (ByteBuf) msg;
        byte[] result1 = new byte[result.readableBytes()];
        result.readBytes(result1);
        System.out.println("Server said:" + new String(result1));
        result.release();
    }

    /**
     * 本方法用于处理异常
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }


    /**
     * 本方法用于向服务端发送信息
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String msg = "hello Server!";
        ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());
        encoded.writeBytes(msg.getBytes());
        ctx.write(encoded);
        ctx.flush();
    }
}

5、运行结果:

服务端:

SimpleServerHandler.channelRead
Client said:hello Server!
客户端:
SimpleClientHandler.channelRead

Server said:hello client!

 

后话:

      好了,看完整篇博文,复制代码,跑起来了!然后你也有了解决问题的思路,不管对错能解决问题了。但是每个对象,每个参数是什么意思能搞懂吗?博主不才,虽然博主后来再次添加了很多注释,但是对很多对象以及参数理解不深,也不知道自己的用法是否是出于设计者本意!

      所以,下一篇博主会稍微深入一些介绍对象以及使用,会让你更加了解自己使用的这个NIO中间件工具,若是想成为专家大牛,就自己去下载源码分析吧~博主目前在拓展知识体系阶段,还没有如此多精力去深入,这个需要很多技巧。

    废话不多说了,下一篇地址:https://blog.csdn.net/the_fool_/article/details/80623299

 

posted @ 2018-06-07 16:35  黑猫先生  阅读(1964)  评论(0编辑  收藏  举报