Netty channel.write not writing message

前端 未结 1 622
不思量自难忘°
不思量自难忘° 2021-01-12 04:44

I\'m trying to make my first steps with Netty, for this purpose I wrote simple server on Netty and simple client on oio plain TCP.

Client sends random text packet, a

相关标签:
1条回答
  • 2021-01-12 05:40

    You will see why it fails if you replace your

    ctx.write("Ack"); 
    ctx.flush();
    

    in your serverHandler with the following:

    ChannelFuture cf = ctx.write("Ack");
    ctx.flush();
    if (!cf.isSuccess()) {
        System.out.println("Send failed: " + cf.cause());
    }
    

    It should give you a message saying that String is not supported.

    ByteBuf should work though:

    ctx.write(Unpooled.copiedBuffer("Ack", CharsetUtil.UTF_8));
    ctx.flush();
    

    on the client side edit the channelRead method:

    ByteBuf in = (ByteBuf) msg;
    System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));
    
    0 讨论(0)
提交回复
热议问题