netty游戏服务器搭建之客户端

由于本人没有游戏开发经验,所以用java模拟了一下游戏客户端

首先写client的主函数

public class NettyClient {
    /*
     * 服务器端口号
     */
    private int port;

    /*
     * 服务器IP
     */
    private String host;

    public NettyClient(int port, String host)throws InterruptedException {
        this.port = port;
        this.host = host;
        start();
    }
    
    private void start() throws InterruptedException {
        
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        
        try {
            
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.group(eventLoopGroup);
            bootstrap.remoteAddress(host, port);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel)
                        throws Exception {                    
                    //socketChannel.pipeline().addLast(new NettyClientHandler());//客户端业务处理handler,先注释掉
                }
            });
            ChannelFuture future = bootstrap.connect(host, port).sync();
            if (future.isSuccess()) {
                SocketChannel socketChannel = (SocketChannel) future.channel();
                System.out.println("----------------connect server success----------------");
            }
            future.channel().closeFuture().sync();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
    public static void main(String[] args) throws InterruptedException {
        
        NettyClient client = new NettyClient(8012,"localhost");

    }

   启动函数:控制台输出

  ----------------connect server success----------------

 服务器控制台输出:

clinet编号:f73761a3加入服务器

关闭客户端,服务器控制台输出

clinet:f73761a3 离开服务器


至此,客户端已经连上了服务器,下面写客户端的业务handler

public class NettyClientHandler extends ChannelInboundHandlerAdapter{
     private  ByteBuf firstMessage;
    
    public NettyClientHandler(){
        
    }
    
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {//向服务器发送消息
        byte[] data = "服务器,给我一个APPLE".getBytes();
        firstMessage=Unpooled.buffer();
        firstMessage.writeBytes(data);
        ctx.writeAndFlush(firstMessage);
    }
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        String rev = getMessage(buf);
        System.out.println("客户端收到服务器数据:" + rev);
    }
    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;
        }
    }
}

   将之前注释的解开

    socketChannel.pipeline().addLast(new NettyClientHandler());//客户端业务处理handler

    再次运行client,服务器控制台输出

   clinet编号:54f2b2d5加入服务器
   服务端收到客户端数据:服务器,给我一个APPLE

    客户端控制台输出

   客户端收到服务器数据:我收到了,apple给你,succcess


  




  

猜你喜欢

转载自blog.csdn.net/qw463800202/article/details/53506372