简体   繁体   English

使用Java检查开放端口

[英]Check for open ports with Java

使用Java,如何检查特定的TCP / IP端口是否已打开并且未被防火墙阻止?

If by "port is open" you mean, that this port can be used by your server application, then you can just do: 如果“打开端口”是指该端口可以被服务器应用程序使用,那么您可以执行以下操作:

try {
    serverSocket = new ServerSocket(port);
} catch (IOException e) {
    System.out.println("Could not listen on port: " + port);
    // ...
}

IOException will be thrown, if you cannot open server socket on this port. 如果无法在此端口上打开服务器套接字,则将引发IOException。

If by "not blocked by a firewall" you mean, that this port can be accessed from hosts outside your network, then there's no straight way to check this without trying to open connection to your host:port from outside network. 如果“不受防火墙阻止”是指可以从网络外部的主机访问此端口,则没有直接方法可以检查此端口,而无需尝试从外部网络打开与host:port的连接。 There may be other firewalls / NATs between host where your service is started, and host, which may try to connect to you service. 启动服务的主机与主机之间可能还有其他防火墙/ NAT,它们可能会尝试连接到您的服务。

There are some common techniques which allow to check service accessibility from outside network, see, for example, NAT traversal . 有一些通用技术可以检查来自外部网络的服务可访问性,例如,参见NAT遍历

Check if a port is open for inbound connections? 检查端口是否为入站连接打开? You'll have to ask the firewall. 您必须询问防火墙。 Suppose, you listen to a port but this port is blocked by the firewall, then you'll wait "forever". 假设您侦听一个端口,但是此端口已被防火墙阻止,那么您将“永远”等待。

The only way out - run a small server outside your network/your machine and ask it remotly to establish a connection to a given port on your machine. 唯一的出路-在您的网络/计算机外部运行一台小型服务器,并远程请求该服务器与计算机上的给定端口建立连接。 The remote server can reply (on a different channel) if it was able to connect or not. 远程服务器是否可以连接(可以在其他通道上)。

Another idea: use one of the many port test services on the web. 另一个想法:使用网络上的许多端口测试服务之一。 googling "port test online" gives some results. 谷歌搜索“在线端口测试”给出了一些结果。

Socket allows connection between 2 machines in the network. 套接字允许网络中2台计算机之间的连接。 There may be several filrewalls on this way: 用这种方式可能有几个filrewall:

  1. personal firewall on both sides 两侧都有个人防火墙
  2. firewalls of the companies on both sides. 双方公司的防火墙。
  3. firewalls of the ISPs on both sides. 双方的ISP的防火墙。

Firewalls may be configured to block 防火墙可以配置为阻止

  1. certain IPs 某些IP
  2. certain ports 某些港口
  3. certain protocols 某些协议
  4. traffic direction (in/out bound) 交通方向(出入限制)

Moreover 2 different situations look the same from TCP point of view: 此外,从TCP的角度来看,有两种不同的情况是相同的:

  1. server is not listening to the port 服务器未监听端口
  2. firewall blocks the connection (see above) 防火墙阻止连接(请参见上文)

Shortly first you have to decide what do you want to test. 很快,首先您必须决定要测试什么。 If for example you just want to know that you can connect to specific port on specific machine call new Socket(host, port) and catch exception. 例如,如果您只想知道可以连接到特定计算机上的特定端口,请调用new Socket(host, port)并捕获异常。 If you want to distinguish between situation that firewall bothers you or the remote has does not respond it is not enough. 如果要区分防火墙困扰您或远程服务器没有响应的情况,这是不够的。

In this case you need some other reference. 在这种情况下,您需要其他参考。 For example you know that remote host has HTTP server on it and some other proprietary server that can be blocked by firewall you can first establish HTTP connection (to check that host is alive) and then try to connect to the socket. 例如,您知道远程主机上有HTTP服务器,而防火墙可能阻止了其他一些专有服务器,则可以首先建立HTTP连接(以检查主机是否还活着),然后尝试连接到套接字。 If HTTP works and socket does not probably firewall is blocking it. 如果HTTP有效并且套接字可能不会被防火墙阻止。

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

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