Ian*_*edv 6 java tcp client-server netty
我正在尝试用Netty编写一个简单的echo服务器.我正在阅读Netty in Action MEAP v8来了解一些理论并学习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();
    }
}
Run Code Online (Sandbox Code Playgroud)
和客户端处理程序:(我确实尝试将'\ r \n'附加到已发送的消息,但这没有什么区别,我在这里找到:Netty Client to Server消息)
@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();
    }
}
Run Code Online (Sandbox Code Playgroud)
服务器:
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();
    }
}
Run Code Online (Sandbox Code Playgroud)
服务器处理程序:
@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();
    }
}
Run Code Online (Sandbox Code Playgroud)
它必须是我想念的小东西,所以任何帮助都将保持我的短暂理智,并将非常感激!
而不是在ClientHandler 中write使用writeAndFlush:
public void channelActive(ChannelHandlerContext ctx) {
    System.out.println("Connected");
    ctx.writeAndFlush(Unpooled.copiedBuffer("Netty MAY rock!", CharsetUtil.UTF_8));
}
Run Code Online (Sandbox Code Playgroud)