Netty收到时会丢失最新消息

时间:2014-09-25 00:26:21

标签: java tcp netty

我写了一个简单的消息传输和接收。 如果我发送的消息很少,一切都很好。如果我发送了大量消息,则不处理后者。

如果我发送100 Id,我会得到所有 1 2 3 4 5 6 7 ... 100
如果我发送1000 Id,我得到1 ... N(N <1000)
1 2 3 4 5 6 7 ... 958 959 960
1 2 3 4 5 6 7 ... 448 449 450
1 2 3 4 5 6 7 ... 652 653 654

服务器

    public class ServerTCP {

    private AmountServer server;

    public ServerTCP(int _PORT, AmountServer _server) {
        final int PORT = _PORT;
        server = _server;

        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.AUTO_READ, true)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new ServerHandler(server));
                }
            });

            // Start the server.
            ChannelFuture f = b.bind(PORT).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } catch (InterruptedException ex) {
            Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            // Shut down all event loops to terminate all threads.
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

服务器处理程序读

@Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        while (in.isReadable()) {
            int type = in.readInt();
            int id = in.readInt();
            System.out.println(id);
            int amn = in.readLong();
        }

        in.clear();
        in.release();
    }

客户端

   public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 500)
                    .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new ClientHandler());
                }
            });

            ChannelFuture f = b.connect(HOST, PORT).sync();

            int i = 0;

          while (i < 1005) {
                i++;
                ByteBuf firstMessage = Unpooled.buffer(AccountServiceClient.SIZE);
                firstMessage.writeInt(1); //Const
                firstMessage.writeInt(i); //Id
                firstMessage.writeLong(1L);

                System.out.println("Step " + i);
                f.channel().writeAndFlush(firstMessage);
                f.channel().flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Shut down the event loop to terminate all threads.
            group.shutdownGracefully();
        }
    }
}

请原谅我的英文

1 个答案:

答案 0 :(得分:0)

听起来您希望您的消息交换更可靠。考虑引入一些握手或某种机制来防止客户过早退出。此外,它看起来不像客户端正确关闭套接字。我会尝试将这个简单的用例建模到netty echo example,以确保覆盖您的基础。