简体   繁体   English

是否可以在不连接IP端口的情况下检查IP端口是否打开?

[英]Can I check if an IP port is open or not without connecting to the port?

So what I am trying to do is scan a local network for a specific port (4444) and then return whether or not that port is open or not. 因此,我要尝试的是扫描本地网络中的特定端口(4444),然后返回该端口是否打开。 What I'm doing right now is this: 我现在正在做的是这样的:

    private static boolean portIsOpen(String ip, int port, int timeout) {
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(ip, port), timeout);
            socket.close();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

I'm going to be using this method to search for servers on my local network (Think Minecraft's multiplayer and how it scans the local network for open games). 我将使用这种方法在本地网络上搜索服务器(请考虑Minecraft的多人游戏,以及它如何扫描本地网络中的开放游戏)。 My problem right now, is that the server side breaks when I use this method because the server just sends data right away when anything connects to it. 我现在的问题是,当我使用此方法时,服务器端会中断,因为服务器会在有任何连接时立即发送数据。 Is it possible to check whether or not a port is open without connecting to it? 是否可以在不连接端口的情况下检查端口是否打开?

Is it possible to check whether or not a port is open without connecting to it? 是否可以在不连接端口的情况下检查端口是否打开?

In a word, No. 一言以蔽之。

There is no standard mechanism that supports this. 没有支持此功能的标准机制。 The only way you could do this is to design, implement and deploy a custom service to do this ... on all machines on the local network. 您可以执行此操作的唯一方法是设计,实施和部署自定义服务,以在本地网络上的所有计算机上执行此操作。


As EJP points out, a well-written service should be able to cope with a client that immediately closes the TCP connection without sending or receiving data. 正如EJP所指出的那样,编写良好的服务应该能够应对无需发送或接收数据即刻关闭TCP连接的客户端。

But if your aim is to do this without anyone noticing the probes ... good luck with that! 但是,如果您的目的是在没有任何人注意探针的情况下进行此操作,那么祝您好运!

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

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