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.
You've just finished your first server on top of Netty. Now that we have written our first server, we need to test if it really works. The easiest way to test it is to use the telnet command. For example, you could enter telnet localhost 8080 in the command line and type something. However, can we say that the server is working fine?
Netty has been designed carefully with the experiences learned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.
The latest version of Netty is available in the project download page. To download the right version of JDK, please refer to your preferred JDK vendor's web site. As you read, you might have more questions about the classes introduced in this chapter.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With