netty之 hello word

这篇文章介绍netty的最基本的输出 hello word。

该文章根据 并发编程网的译文进行实现 https://ifeve.com/netty5-user-guide/

首先引入包:

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>5.0.0.Alpha2</version>
</dependency>

1.ServerHandler

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHandler extends ChannelHandlerAdapter {


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 出现异常
        System.out.println( cause.toString() );
        ctx.close();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("netty is register...");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("netty is active...");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buff = (ByteBuf) msg;
        byte [] data = new byte[buff.readableBytes()];
        buff.readBytes(data);
        System.out.println("收到客户端发来的消息: " + new String(data).trim() );
        String reps = "我收到了你发来的消息";
        // 返回消息
        ctx.writeAndFlush(Unpooled.copiedBuffer(reps.getBytes()));
    }
}

2.Server

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {
    public static void main(String[] args) throws InterruptedException {
        //1 创建线两个程组
        //一个是用于处理服务器端接收客户端连接的
        //一个是进行网络通信的(网络读写的)
        NioEventLoopGroup pGroup = new NioEventLoopGroup();
        NioEventLoopGroup cGroup = new NioEventLoopGroup();

        //2.创建辅助工具类,用于服务器通信的一系列配置
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(pGroup,cGroup) // 绑定两个线程组
        .channel(NioServerSocketChannel.class) // 指定NIO模式
        .option(ChannelOption.SO_KEEPALIVE , true) // 设置保持连接
        .childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel sc) throws Exception {
                // 3. 在这里配置具体的数据处理方法
                sc.pipeline().addLast(new ServerHandler());
            }
        });

        //4 进行绑定
        ChannelFuture cf1 = bootstrap.bind(8888).sync();
        //ChannelFuture cf2 = b.bind(8764).sync();
        //5 等待关闭
        cf1.channel().closeFuture().sync();
        //cf2.channel().closeFuture().sync();
        pGroup.shutdownGracefully();
        cGroup.shutdownGracefully();
    }
}

3.ClientHandler

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.Date;

public class ClientHandler extends ChannelHandlerAdapter {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println( cause.toString() );
        System.out.println("出错了...");
        ctx.close();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println( "注册成功..." );
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println( "激活成功..." );
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf)msg;
        byte [] data = new byte[buf.readableBytes()];
        buf.readBytes(data);

        System.out.println(  "收到了消息:" + new String(data).trim() );
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("我读完了...");
        /*String reps = "客户端收到了服务端发来的消息..." + new Date();
        ctx.writeAndFlush(Unpooled.copiedBuffer(reps.getBytes()));*/
    }
}

4.Client

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {
    public static void main(String[] args) throws InterruptedException {

        // 创建线程组
        NioEventLoopGroup group = new NioEventLoopGroup();

        // 创建辅助工具类
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group) //绑定线程组
                .channel(NioSocketChannel.class) // 指定 NIO 模式
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new ClientHandler()); // 在这里配置数据接收处理的方法(类)
                    }
                });

        // 绑定连接地址
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1",8888).sync();

        // 向服务端发送消息
        channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer("你好,我是 netty".getBytes()));

        channelFuture.channel().closeFuture().sync();
        group.shutdownGracefully();
    }
}

 

测试:

 先运行 Server,再运行 Client

可以看到 Client 发送的消息 Server收到了,并且返回了消息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值