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

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

问题描述

我正在尝试使用Netty的第一步,为此我在Net上编写了简单的服务器,在oio plain 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.

客户端随机发送文本包,并且必须接收确认消息。
请参阅处理程序方法:

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 incoming message as echo - it works. When i send thomething else - it don'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 some thing 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.

推荐答案

如果您更换

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

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天全站免登陆