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