简体   繁体   中英

Receive message from server netty nio java

I am trying to figure out how can i create a chat using netty nio server and client code found from here . What I want actually is to figure out how exactly can I use the received messages from both client and server. I have found in the server the following code in the server which types a received message:

 public class EchoClientHandler extends ChannelInboundHandlerAdapter {

   public String message;
   @Override
   public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      System.out.println("Got reply: " + msg.toString().trim());
      message = msg.toString().trim();
      ctx.disconnect();
    }
    // How to store the message variable and use it from my main function?????
 }

This code stands for the echoClient:

public class EchoClient {

  public String host;
  public int port;


  public EchoClient(String host, int port) {

     this.host = host;
     this.port = port;
  }

  public void send(String msg) throws InterruptedException {

      EventLoopGroup eventGroup = new NioEventLoopGroup();

      Bootstrap bootstrap = new Bootstrap();
      bootstrap.group(eventGroup)
            .remoteAddress(host, port)
            .channel(NioSocketChannel.class) // TCP server socket
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    socketChannel.pipeline().addLast(
                            // break stream into "lines"
                            new LineBasedFrameDecoder(EchoServerHandler.LINE_MAX, false, true),
                            new StringDecoder(CharsetUtil.UTF_8),
                            new StringEncoder(CharsetUtil.UTF_8),
                            new EchoClientHandler()
                    );
                }
            });

      ChannelFuture f = bootstrap.connect().sync();
      System.out.println("Connected!");
      f.channel().writeAndFlush(msg).sync();
      System.out.print("Sent: " + msg);
      f.channel().closeFuture().sync();
      eventGroup.shutdownGracefully().sync();
      System.out.println("Done.");
  }

  public static void main(String[] args) throws InterruptedException {

      EchoClientHandler temp = new EchoClientHandler(); //how can i have access to this variable and the returned message?
      String host ="127.0.0.1";
      int port = 8080;
      EchoClient client = new EchoClient(host, port);
      client.send("Hello world!\n");
      temp.message?
  }
}

In the code here there is a printing message, however how can the Object msg is connected with the received message? And how can i use it when i want?

EDIT: public String message in the channelRead has a value however, inside the main is null. How can I pass correctly the value? Guess that the EchoCLientHandler and EchoClient are different threads than my main function in EchoClient. However, is there a way to read from my main function the received message that got from EchoClientHandler?

You used the StringEncoder to encode the incoming ByteBuf buffer, StringDecoder to decode the outgoing message. You may want to deal with it similar with bgTaskGroup.submit(new Sleeper(sleepMillis, ctx.channel())); in BGTaskServerHandler handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM