2014-09-25 77 views
-1

我寫了一個簡單的消息發送和接收。 如果我發幾條消息,一切都很好。如果我發送很多消息,則不處理後者。Netty在收到時丟失最新消息

如果我發送100個ID我得到所有。
1 2 3 4 5 6 7 ... 100
如果我發送1000標識我得到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(); 
     } 
    } 
} 

原諒我的英語

回答

0

這聽起來像你想你的信息交換更可靠。考慮引入一些握手或某種機制來防止客戶過早退出。另外,它看起來並不像客戶端正確關閉套接字。我會嘗試將這個簡單用例更密切地模擬到netty echo example,以確保您覆蓋了您的基礎。

+0

謝謝! 我很快關閉了客戶端上的套接字 – user2413972 2014-09-25 07:25:17