Java 简单Netty Echo服务器/客户端未接收消息

Java 简单Netty Echo服务器/客户端未接收消息,java,tcp,client-server,netty,Java,Tcp,Client Server,Netty,我正试图用Netty编写一个简单的echo服务器。我读这本书是为了了解一些理论,学习Netty的核心基础知识。客户端已成功连接,但没有来自客户端的消息。我能够通过telnet将消息发送到服务器并接收响应,因此我猜问题出在客户端,我只是不知道出了什么问题,因为我是Netty的新手 以下是客户: public class Client { private final String host; private final int port; public Client(St

我正试图用Netty编写一个简单的echo服务器。我读这本书是为了了解一些理论,学习Netty的核心基础知识。客户端已成功连接,但没有来自客户端的消息。我能够通过telnet将消息发送到服务器并接收响应,因此我猜问题出在客户端,我只是不知道出了什么问题,因为我是Netty的新手

以下是客户:

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

这一定是我错过的小东西,所以任何帮助都会让我保持短暂的理智,我将不胜感激

在您的客户端句柄中使用
writeAndFlush
而不是
write

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

谢谢,成功了!还有@NormanMaurer尊敬作者先生,等待下一次更新!
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();
    }
}
@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();
    }
}
public void channelActive(ChannelHandlerContext ctx) {
    System.out.println("Connected");
    ctx.writeAndFlush(Unpooled.copiedBuffer("Netty MAY rock!", CharsetUtil.UTF_8));
}