1
votes

I have a simple netty socket server and simple client with standart socket implementation(no netty). from client to server I can send string via PrintWriter toServer = new PrintWriter(client.getOutputStream(), true); and in client I have BufferedReader fromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));. in Server side I have this code

@Override
protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
  String msg = message;
  String response = "Response from server";
  try {
   System.out.println(msg);
   System.out.flush();
   ctx.writeAndFlush(response);
  } finally {
      ReferenceCountUtil.retain(msg);
  }
}

But I can't read any string in client side from server. How I can send string from netty to client in right way? client side:

toServer = new PrintWriter(client.getOutputStream(), true);
fromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));

for (int i = 0; i < 6; i++) {
    toServer.println("simple string: " + i);
    if ((line = fromServer.readLine()) != null) {
        System.out.println("Responce from server: " + line);
    }
}
1

1 Answers

0
votes

Try this:

ctx.channel().writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));

Letme know if works.