Netty:在 ctx.flush() 之后保持服务器套接字打开

Netty: Keep Server Socket Open After ctx.flush()

我有两台服务器"A"(由我的朋友构建)和"B"(由我构建,使用Netty 4.1)。当客户端发送命令时,此服务器 "A" 和 "B" 会 return 一些响应。我尝试使用简单的 JAVA 客户端套接字访问该服务器。下面是 java 客户端代码:

public class SocketClient
{
    public void run()
    {
        try {
            ClassLoader classLoader = getClass().getClassLoader();
            File file = new File(classLoader.getResource("request.txt").getFile());
            String content = new String(Files.readAllBytes(file.toPath()));

            System.out.println("Connecting to server:8888");
            Socket socket = new Socket("myserver.com", 8888);
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            out.write(content.getBytes());
            out.flush();

            while(true) {
                int response = in.read();
                System.out.println(response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

当客户端连接到服务器 A(非 netty)时,read() 方法给我这样的输出:

48
48
57
56
48
50
49
48

但是,当我尝试连接到服务器 "B"(使用 netty)时,输出是这样的:

48
48
57
56
48
50
49
48
-1
-1
-1
-1

为什么会这样?顺便说一句,在服务器 B 中,我正在使用 ctx.write() and ctx.flush() 以及如何使服务器 B 获得与服务器 A 相同的行为(不关闭连接,所以它不会 return -1)

编辑:附加信息

实际上在服务器 "B" 中,我正在使用 ChannelInboundHandlerAdapterChannelOutboundHandlerAdapter。做了一些实验后,问题出在 ChannelOutboundHandlerAdapter 上。当我使用 ctx.writeAndFlush()InboundAdapter 发送响应时,套接字没有关闭。但是当响应被传递到 OutboundAdapter 然后我使用 ctx.writeAndFlush()OutboundAdapter write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) 方法中发送响应。套接字已关闭

这是我的样本 OutboundAdapter:

public class OutboundHandler extends ChannelOutboundHandlerAdapter {

    private static final Logger logger = LogManager.getLogger(OutboundHandler.class);

    //Sending or forwarding message to channel which is already configured
    //condition is depends on which model instance
    //packaging is worked on its packager class
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
    {
        String response = "Response form OutboundHandler";
        ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
    } }

谢谢

在 Netty 服务器实现上,例如,也许你必须通过 ChannelHandlerContext 更正确地处理 reading/writing,而不是使用 ctx.write() ctx.channel().writeandflush() 应该是更正确的方法,在关闭用法中也是如此ctx.channel().close()ctx.channel().close().sync() 可能会有所不同...

您没有检查流的结尾,所以您无休止地回显它。 read() returns 一个 。核实。它 returns -1 在流的末尾。就这样结束了。完成。完成。停在那里。不要打印它。不要继续阅读。不要通过 GO。不收$200.

调试了一整天,终于找到了问题的根源。我不小心把 ctx.close() 放在我的 InboundAdapter 的某个地方,所以当我将消息传递到 OutboundAdapter 并将消息刷新到客户端时,它将关闭连接

try {
   //some code
}
} catch (Exception ex) {
        logger.error(ex);
        ErrorData error = new ErrorData();
        error.setCode("99");
        error.setMessage("General System Error");

        //pass the message to OutboundAdapter
        ctx.writeAndFlush(error);
} finally {
  ctx.close();
}

所以,我只需要删除 ctx.close() 并且服务器停止关闭套接字连接