Netty - 客户端读写操作

我正在尝试从服务器发送和接收消息。服务器已建立在远程环境中。

服务器有一些特定的信号代码,例如:

http://img.mukewang.com/6178b0b400017e3807740262.jpg

这些代码作为字节发送。在这一点上,问题开始了。


当客户端与服务器连接时,服务器立即发送 100000050 并在几毫秒后发送 10000051。


我应该在收到 10000051 后发送 10000060 但我不知道如何发送。


客户端.JAVA

public static void main(String[] args) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();

        try {

            Bootstrap clientBootstrap = new Bootstrap();


            clientBootstrap.group(group);

            clientBootstrap.channel(NioSocketChannel.class);

            clientBootstrap.remoteAddress(new InetSocketAddress("10.80.15.70", 55102));


            clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {

                protected void initChannel(SocketChannel socketChannel) throws Exception {

                    socketChannel.pipeline().addLast(new ClientHandler());

                }

            });

            ChannelFuture channelFuture = clientBootstrap.connect().sync();


            channelFuture.channel().closeFuture().sync();

        }

        finally {

            group.shutdownGracefully().sync();

        }

    }

客户端处理程序.JAVA

public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private static final Logger LOGGER = Logger.getLogger( ClientHandler.class.getName() );


    @Override

    public void channelActive(ChannelHandlerContext channelHandlerContext){

        //System.out.println("\nhelloo");

        //channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8));

        byte [] test = new byte[9];


        test[0] = 0;

        test[1] = 6;

        test[2] = 0;

        test[3] = 0;

        test[4] = 0;

        test[5] = 0;

        test[6] = 0;

        test[7] = 0;

        test[8] = 1;



        //byte [] message = "100000060".getBytes();

        channelHandlerContext.writeAndFlush(test);

    }


    @Override

    public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {

        System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));

    }


慕森王
浏览 166回答 1
1回答

BIG阳

您可以通过更新您的channelRead0方法来检查收到的消息,然后在收到您正在寻找的消息时回复。@Overridepublic void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {&nbsp; &nbsp; String message = in.toString(CharsetUtil.UTF_8);&nbsp; &nbsp; if (message.equals("10000051")) { //Now do what you were doing in channel active&nbsp; &nbsp; &nbsp; &nbsp; byte [] test = new byte[9];&nbsp; &nbsp; &nbsp; &nbsp; test[0] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[1] = 6;&nbsp; &nbsp; &nbsp; &nbsp; test[2] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[3] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[4] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[5] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[6] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[7] = 0;&nbsp; &nbsp; &nbsp; &nbsp; test[8] = 1;&nbsp; &nbsp; &nbsp; &nbsp; //byte [] message = "100000060".getBytes();&nbsp; &nbsp; &nbsp; &nbsp; channelHandlerContext.writeAndFlush(test);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java