Simple Netty Echo Server / Client Does Not Receive Messages

I am trying to write a simple echo server with Netty. I read Netty in MEAP v8 action to get some theory and learn the basics of Netty. The client connects successfully, but the messages do not reach the client. I can telnet the message to the server and get a response, so I think the problem is on the client, I just don’t know what is wrong, due to the fact that I am new to Netty.

Here is the client:

public class Client { private final String host; private final int port; public Client(String host, int port) { this.host = host; this.port = port; } public void start() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .remoteAddress(new InetSocketAddress(host, port)) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture f = b.connect().sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } public static void main (String [] args) throws Exception { new Client("127.0.0.1", 11235).start(); } } 

And the client handler: (I tried adding "\ r \ n" to the sent message, but that did not affect what I found here: Netty Client for Server Message )

 @Sharable public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { public void channelActive(ChannelHandlerContext ctx) { System.out.println("Connected"); ctx.write(Unpooled.copiedBuffer("Netty MAY rock!", CharsetUtil.UTF_8)); } protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception { System.out.println( "Client received: " + in.toString(CharsetUtil.UTF_8)); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } } 

Server:

 public class EchoServer { private final int port; public EchoServer(int port) { this.port = port; } public void start() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { System.out.println("New client connected: " + ch.localAddress()); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture f = b.bind().sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } public static void main (String [] args) throws Exception { new EchoServer(11235).start(); } } 

Server Handler:

 @Sharable public class EchoServerHandler extends ChannelInboundHandlerAdapter { public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf in = (ByteBuf) msg; System.out.println( "Server received: " + in.toString(CharsetUtil.UTF_8)); ctx.write(in); } public void channelReadComplete(ChannelHandlerContext ctx) { ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) .addListener(ChannelFutureListener.CLOSE); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } } 

It should be something small that I am missing, so any help will save my fleeting sanity and will be very grateful!

+6
source share
2 answers

Instead of write use writeAndFlush in your ClientHandler:

 public void channelActive(ChannelHandlerContext ctx) { System.out.println("Connected"); ctx.writeAndFlush(Unpooled.copiedBuffer("Netty MAY rock!", CharsetUtil.UTF_8)); } 
+6
source

If we add below line to EchoClientHandler

Protected void channelRead0 (ChannelHandlerContext ctx, ByteBuf in) throws an exception {System.out.println ("Client received:" + in.toString (CharsetUtil.UTF_8)); channelHandlerContext.writeAndFlush (Unpooled.copiedBuffer ("Netty Rocks second time", CharsetUtil.UTF_8));

 } 

it is not a print on the server. Also, please advise how the synchronous client and server exchange messages. Thank you in advance

0
source

Source: https://habr.com/ru/post/974967/


All Articles