【问题标题】:Netty - Client Write & Read OperationNetty - 客户端读写操作
【发布时间】:2018-10-03 23:18:40
【问题描述】:

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

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

这些代码以 字节 的形式发送。此时,问题开始了。

当客户端与服务器连接时,服务器立即发送 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());
    }*/
}

谢谢。

【问题讨论】:

    标签: java sockets netty


    【解决方案1】:

    您可以通过更新您的 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);
        }
    }
    

    【讨论】:

    • 不起作用。也许我需要实现 outboundhandler?
    • 不,我刚刚修好了。问题是我的信息。现在一切正常。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2012-01-16
    • 2018-07-10
    • 2013-01-19
    • 2015-07-04
    • 1970-01-01
    相关资源
    最近更新 更多