简体   繁体   English

我的Netty客户端如何检测我的Netty服务器是否侦听端口(可用)?

[英]How can I have my netty client detect if my netty server listens on a port (is available)?

I have a netty client that works fine if the server application is listening. 我有一个netty客户端,如果服务器应用程序正在侦听,则可以正常工作。 But if the server application is not running and therefore not listening on the port, the client still runs as if it expects the server to be there. 但是,如果服务器应用程序未在运行,因此未在端口上进行侦听,则客户端仍会像期望服务器在那一样运行。

I would like to handle the case when the server is not listening and in such a case not even send the data (and do something else instead, like telling the user to contact an admin or so). 我想处理服务器不监听的情况,在这种情况下甚至不发送数据(而是执行其他操作,例如告诉用户联系管理员等)。 Is this even possible? 这有可能吗?

Note: If the client attempts to connect to an unspecified port or ip, my client throws exceptions which I can handle. 注意:如果客户端尝试连接到未指定的端口或ip,则客户端会抛出我可以处理的异常。 My issue is that no exceptions are thrown when the server is not listening on the port it should normally listen to. 我的问题是,当服务器不在通常应侦听的端口上侦听时,不会引发任何异常。

Yeap. 是的 Sometimes you can't detect that server is down/not listening on port anymore. 有时您无法检测到服务器已关闭/不再监听端口。

So for that case, you may add to your client pipeline ReadTimeoutHandler . 因此,在这种情况下,您可以将ReadTimeoutHandler添加到客户端管道ReadTimeoutHandler Which will throw ReadTimeoutException in case your client didn't get any message from the server within the certain period of time. 万一您的客户端在一定时间内没有收到来自服务器的任何消息,它将抛出ReadTimeoutException However, this may be not enough. 但是,这可能还不够。 For example, when your connection between server and client is not very active this exception could be thrown too. 例如,当服务器和客户端之间的连接不是非常活跃时,也可能引发此异常。 So for that case, you may need to implement ping-pong handlers, that will tell for your ReadTimeoutHandler that connection is active and it will not throw the exception in that case. 因此,在这种情况下,您可能需要实现乒乓处理程序,这将为您的ReadTimeoutHandler告知连接处于活动状态,并且在这种情况下不会引发异常。

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

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