簡體   English   中英

Netty服務器消息未發送到客戶端

[英]Netty server message not making it to client

我正在嘗試netty,但是響應時客戶端似乎沒有收到我的消息。 我使用Java NIO和socketChannel編寫了相同的代碼,看起來還不錯。 我之所以這樣說,是因為客戶端記錄了我發送的所有日志。 下面是我的服務器

public class Server{

    public void init(final int port){

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);  
    EventLoopGroup workerGroup = new NioEventLoopGroup(); 

    try {

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new ServerHandler());
                    }
                })

                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = bootstrap.bind("localhost", port).sync(); 

        f.channel().closeFuture().sync(); 

    } catch (InterruptedException e) {
        System.out.println("Server encountered an InterruptedException");
        e.printStackTrace();

    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

下面是我的服務器處理程序

public class ServerHandler extends ChannelInboundHandlerAdapter {

public static Logger LOG =  LoggerFactory.getLogger(UTPServerHandler.class);
final int LEN = 1028;
private ByteBuf byteBuf;
private MyDecoder myDecoder;

@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    byteBuf     = ctx.alloc().buffer(MAX_PACKET_LENGTH);
    myDecoder = new MyDecoder();
}

@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
    byteBuf.release();
    byteBuf = null;
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {

    byteBuf = (ByteBuf) msg;
    myDecoder.decodeMessage(byteBuf, ctx); //This is where I try to decode which message and respond. I pass ctx along so I know which to reponsd to. See sample response below
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    System.out.println"Got an exception...");
    ctx.close();
}

}

樣品回復:

 public static void dummyResponse(ChannelHandlerContext ctx, int test){

    ByteBuf response = Unpooled.buffer(32);
    response.writeByte(test);
    response.writeByte(test);
    response.writeByte(test);
    response.writeByte(test);
    response.writeByte(test);      
    ctx.channel().writeAndFlush(response);
}

我在盒子上做一個tcpdump,我看到網上發送的是什么,所以我不確定自己做錯了什么。 另外,幾分鍾后,這將被記錄下來,我不確定我在服務器或服務器處理程序中正在做什么,以抱怨此事:

`2016-10-25 16:58:38.974 [nioEventLoopGroup-3-1] | 錯誤| 泄漏:在垃圾回收之前未調用ByteBuf.release()。 啟用高級泄漏報告以找出泄漏發生的位置。 要啟用高級泄漏報告,請指定JVM選項'-Dio.netty.leakDetection.level = advanced'或調用ResourceLeakDetector.setLevel()有關更多信息,請參見http://netty.io/wiki/reference-counted-objects.html 。 。

請任何幫助或建議。 在此先感謝`

之所以會收到關於釋放的消息,是因為您在處理程序添加的方法中分配了ByteBuf並將其分配給byteBuf,然后在channelRead中將byteBuf分配給了傳入消息,從而導致第一個消息被垃圾回收而從未被釋放。 通過不在handlerAdded中分配它來解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM