Netty channel.write 不写消息 [英] Netty channel.write not writing message

查看:28
本文介绍了Netty channel.write 不写消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Netty 迈出第一步,为此我在 Netty 上编写了简单的服务器,在 oio 普通 TCP 上编写了简单的客户端.

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.

客户端发送随机文本包,必须收到Ack"消息.查看处理程序方法:

Client sends random text packet, and must receive "Ack" message. See the handler method:

  @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

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

    ByteBuf in = (ByteBuf) msg;
    StringBuilder sb = new StringBuilder();
    try {
        while (in.isReadable()) {
            sb.append((char) in.readByte());

        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
    LOG.debug("Incoming message. ACK was send");

    String myaddr = ctx.channel().remoteAddress().toString();
    String message = "Message from " + myaddr + " :" + sb.toString();
    LOG.debug(message);
    sendToOther(myaddr, message);

}

问题是 - 当我尝试发回Ack"字符串时 - 客户端什么也没收到.但是当我尝试发回传入的消息时 - 它工作正常,并且我在我的客户端看到回声.

The problem is - when I try to send back "Ack" string - client receives nothing. But when i try to send back message that incoming - it works fine, and i see echo in my client.

 @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    ctx.write(msg); 
    ctx.flush();

write() 方法需要 Object 并且我尝试发送 (Object) String - 但什么也没发生.我也尝试发送 ByteBuf(我在一篇文章中看到过),但它仍然无法正常工作.

write() method needs Object and I tried to send (Object) String - but nothing happened. I also tried to send ByteBuf (I saw that in one article) and it's still not working too.

当我将传入的消息作为回声发回时 - 它起作用了.当我发送其他东西时 - 它没有.请帮帮我,我就是不知道我的错误在哪里.

When I send back the incoming message as echo - it works. When I send thomething else - it doesn't. Please help me, i just can't figure where my mistake is.

我解决了这个问题.重点是,您只需要发送 ByteBuff.所以我们需要创建它,并向它写入一些东西,然后我们才能将它写入 chanel 管道.就我而言,它就像:

I solved this problem. Point was, that you need to send ByteBuff only. So we need create it, and write something to it and only then we can write it into chanel pipe. in my case it was like :

String ack = "ACK";
    ByteBuf out = ctx.alloc().buffer(ack.length()*2);
    out.writeBytes(ack.getBytes());
    ctx.write(out); 
    ctx.flush();
    LOG.debug("Incoming message. ACK was send");

可能这不是很好的解决方案,但它可以作为示例.

May be this is not exelent sollution, but it works as example.

推荐答案

如果你更换你的,你就会明白为什么它会失败

You will see why it fails if you replace your

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

在您的 serverHandler 中使用以下内容:

in your serverHandler with the following:

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

它应该给您一条消息,指出不支持 String.

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

ByteBuf 应该可以工作:

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

在客户端编辑 channelRead 方法:

on the client side edit the channelRead method:

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

这篇关于Netty channel.write 不写消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆