繁体   English   中英

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

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

我有两个服务器“ A”(由我的朋友建造)和“ B”(由我使用Netty 4.1建造)。 当客户端发送命令时,此服务器“ A”和“ B”将返回一些响应。 我尝试使用简单的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(非网络)时, 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具有相同的行为(不关闭连接,因此它不会返回-1)

编辑:附加信息

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

这是我的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 Server实施上,例如,也许您必须更正确地通过ChannelHandlerContext处理读/写,而不是使用ctx.write() ctx.channel().writeandflush()应该是更正确的方式,同时还要关闭ctx.channel().close()使用ctx.channel().close()ctx.channel().close().sync()可能会有所不同...

您无需检查流的结尾,因此会不断回显它。 read()返回一个 核实。 它在流的末尾返回-1。 到此为止。 菲尼斯 Finito。 到此为止 不要打印。 不要继续阅读。 不要通过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() ,然后服务器就无法关闭套接字连接

暂无
暂无

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

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