簡體   English   中英

Netty-客戶端讀寫操作

[英]Netty - Client Write & Read Operation

我正在嘗試從服務器發送和接收消息。 該服務器已構建在遠程環境中。

服務器具有一些特殊的信號代碼,例如: 在此處輸入圖片說明

這些代碼將作為字節發送。 此時,問題開始了。

當客戶端與服務器連接時,服務器立即發送100000050,並在幾毫秒后發送10000051。

我應該在收到10000051后發送10000060,但我不知道如何發送。

CLIENT.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();
        }
    }

CLIENTHANDLER.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));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause){
        cause.printStackTrace();
        LOGGER.info("Exception Occurred in ChannelHandler A");
        channelHandlerContext.close();
    }

    /*@Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        System.out.println("Client received: " + o.toString());
    }*/
}

謝謝。

您可以通過更新channelRead0方法來檢查收到的消息,然后在收到所需的消息時進行回復來做到這一點。

@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
    String message = in.toString(CharsetUtil.UTF_8);

    if (message.equals("10000051")) { //Now do what you were doing in channel active
        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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM