简体   繁体   English

Netty服务器消息未发送到客户端

[英]Netty server message not making it to client

I am trying out netty but it looks like the client does not get my messages when I respond. 我正在尝试netty,但是响应时客户端似乎没有收到我的消息。 I wrote the same code using Java NIO and socketChannel and it seems fine. 我使用Java NIO和socketChannel编写了相同的代码,看起来还不错。 I say this because the client logs whatever I am sending in it logs. 我之所以这样说,是因为客户端记录了我发送的所有日志。 Below is my server 下面是我的服务器

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

Below is my server handler 下面是我的服务器处理程序

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

} }

sample respond: 样品回复:

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

I am doing a tcpdump on my box and I see what I'm sending on the wire so I'm not sure what I'm doing wrong. 我在盒子上做一个tcpdump,我看到网上发送的是什么,所以我不确定自己做错了什么。 Also, after a few minutes, this is logged and I'm not sure what I'm doing in my server or server handler to make complain about this: 另外,几分钟后,这将被记录下来,我不确定我在服务器或服务器处理程序中正在做什么,以抱怨此事:

`2016-10-25 16:58:38.974 [ nioEventLoopGroup-3-1] | `2016-10-25 16:58:38.974 [nioEventLoopGroup-3-1] | ERROR | 错误| LEAK: ByteBuf.release() was not called before it's garbage-collected. 泄漏:在垃圾回收之前未调用ByteBuf.release()。 Enable advanced leak reporting to find out where the leak occurred. 启用高级泄漏报告以找出泄漏发生的位置。 To enable advanced leak reporting, specify the JVM option '-Dio.netty.leakDetection.level=advanced' or call ResourceLeakDetector.setLevel() See http://netty.io/wiki/reference-counted-objects.html for more information. 要启用高级泄漏报告,请指定JVM选项'-Dio.netty.leakDetection.level = advanced'或调用ResourceLeakDetector.setLevel()有关更多信息,请参见http://netty.io/wiki/reference-counted-objects.html 。 。

Please any help or suggestion will do. 请任何帮助或建议。 Thanks in advance ` 在此先感谢`

You get a message about release because you allocate a ByteBuf and assign it to byteBuf in your handlerAdded method, then in channelRead you assign byteBuf to your incoming message, causing that first one to be garbage collected without ever being released. 之所以会收到关于释放的消息,是因为您在处理程序添加的方法中分配了ByteBuf并将其分配给byteBuf,然后在channelRead中将byteBuf分配给了传入消息,从而导致第一个消息被垃圾回收而从未被释放。 Fix this by not allocating it in handlerAdded. 通过不在handlerAdded中分配它来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM