Netty loses latest messages when receiving

I written a simple transmission and reception of messages. If I send few message, everything is fine. If I send a lot of messages, the latter are not processed.

If I send 100 Id i get all. 1 2 3 4 5 6 7 ... 100 If I send 1000 Id i get 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

Server

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

Server-Handler-Read

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

Client

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

Excuse my English

Answers


It sounds like you want your message exchange to be more reliable. Consider introducing some handshaking or some mechanism to prevent the client from exiting prematurely. Also it doesn't look like the client is closing the socket correctly. I would try to model this simple use case more closely to the netty echo example to make sure you have your bases covered.


Need Your Help

Python inversions counter: slice indicies error

python list compiler-errors slice

I'm trying to write an inversions counter that takes a list of integers as input and outputs the number of inversions. Since it's going to handle large lists, I'm using a divide and conquer recurs...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.