如何检查数据报端口上是否有任何应用程序正在运行?

时间:2016-11-03 09:46:42

标签: datagram radius freeradius

我有一个在每个Datagram Socket上运行的radius服务器列表,并且有一个这些已启动的radius服务器列表。 我有一个要求,我需要检查特定服务器是否已停止,如果停止我需要重新启动它。 以下是我写的代码,但不确定这是否是正确的方法。 请指教。

...

int port = radiusServer.getAggregation()
                        .getAuthenticationPort();
                try {
                    // It will throw IO exception if no application is
                    // running
                    // on that
                    // port.
                    new DatagramSocket(port);
                    LOGGER.info(
                            "There is a server running on the Port number {}.",
                            port);
                } catch (IOException e) {
                    LOGGER.error(
                            "Server is not running on port number {}.",
                            port);
                    startServer(radiusServer);
                }

...

1 个答案:

答案 0 :(得分:0)

以下解决方案正常。

           try {
                    // It wont throw IO exception if no application is
                    // running
                    // on that
                    // port.
                    DatagramSocket ds = new DatagramSocket(port);
                    ds.close();
                    LOGGER.warn(
                            "Server is not running on port number {}. Starting Server",
                            port);
                    startServer(radiusServer);
                } catch (IOException e) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(
                                "There is a server already running on the Port number {}.",
                                port);
                    }
                }