Netty3的入门案例

Netty3服务端的代码

Server.java

 public class Server {
    public static void main(String[] args) {
        //服务类
        ServerBootstrap  serverBootstrap = new  ServerBootstrap();
        //boss线程监听端口,worker线程负责数据读写
        ExecutorService boss = Executors.newCachedThreadPool();
        ExecutorService worker = Executors.newCachedThreadPool();
        //设置niosocket工厂
        serverBootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));
        //设置管道的工厂
        serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeLine = Channels.pipeline();
                pipeLine.addLast("decoder", new StringDecoder());
                pipeLine.addLast("encoder", new StringEncoder());
                pipeLine.addLast("helloHandle", new HelloHandle());
                return pipeLine;
            }
        });
        serverBootstrap.bind(new InetSocketAddress(10102));
        System.out.println("start!!!");
    }

}复制代码

HelloHandle.java

  

public class HelloHandle extends SimpleChannelHandler{
    
    /**
     * 接收消息
     */
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

        System.out.println("messageReceived");
        String s = (String) e.getMessage();
        System.out.println(s);
        
        //回写数据
        ctx.getChannel().write("hi");
        super.messageReceived(ctx, e);
    }

    /**
     * 捕获异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        System.out.println("exceptionCaught");
        super.exceptionCaught(ctx, e);
    }

    /**
     * 新连接
     */
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelConnected");
        super.channelConnected(ctx, e);
    }

    /**
     * 必须是链接已经建立,关闭通道的时候才会触发
     */
    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelDisconnected");
        super.channelDisconnected(ctx, e);
    }

    /**
     * channel关闭的时候触发
     */
    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelClosed");
        super.channelClosed(ctx, e);
    }
    

}复制代码
Netty3客户端代码

Client.java

 public class Client {
    public static void main(String[] args) {
        //客户端类
        ClientBootstrap strap = new ClientBootstrap();
        ExecutorService boss = Executors.newCachedThreadPool();
        ExecutorService worker =Executors.newCachedThreadPool();
        //设置niosocket工厂
        strap.setFactory(new NioClientSocketChannelFactory(boss,worker));
        //设置管道的工厂
        strap.setPipelineFactory(new ChannelPipelineFactory() {
            
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeLine = Channels.pipeline();
                pipeLine.addLast("decoder", new StringDecoder());
                pipeLine.addLast("encoder", new StringEncoder());
                pipeLine.addLast("hiHandle", new HiHandle());
                return pipeLine;
                }
            });
        //连接服务端
        ChannelFuture connect = strap.connect(new InetSocketAddress("127.0.0.1",10102));
        Channel channel = connect.getChannel();
        System.out.println("client start");
        Scanner scanner = new Scanner(System.in);
        while(true){
           System.out.println("请输入");
           channel.write(scanner.next());
        }  
    }
    
}复制代码
HiHandle.java       


public class HiHandle extends SimpleChannelHandler {
    /**
     * 接收消息
     */
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        String s = (String) e.getMessage();
        System.out.println(s);
        super.messageReceived(ctx, e);
    }

    /**
     * 捕获异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        System.out.println("exceptionCaught");
        super.exceptionCaught(ctx, e);
    }

    /**
     * 新连接
     */
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelConnected");
        super.channelConnected(ctx, e);
    }

    /**
     * 必须是链接已经建立,关闭通道的时候才会触发
     */
    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelDisconnected");
        super.channelDisconnected(ctx, e);
    }

    /**
     * channel关闭的时候触发
     */
    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelClosed");
        super.channelClosed(ctx, e);
    }

}复制代码
开始测试

1.启动服务端:

2.启动客户端:(并输入信息)


3.回看服务端


关注51码农网,寻找一起进步的同学。www.51manong.com

51码农网  程序员社群学习打卡集聚地

关注51码农网官方微信公众号:

转载于:https://juejin.im/post/5c90f6c86fb9a070eb26717a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值