代码之家  ›  专栏  ›  技术社区  ›  Charlie Salts

测试端口是否打开并使用PHP转发

  •  16
  • Charlie Salts  · 技术社区  · 15 年前

    完全公开:有一个类似的问题 here .

    是否有任何方法可以测试特定端口是否打开并使用PHP正确转发?具体来说,如何使用套接字连接到具有给定端口的给定用户?

    例如,在的“自定义端口测试”部分 WhatsMyIP.org/ports .

    1 回复  |  直到 11 年前
        1
  •  73
  •   Alix Axel    15 年前

    我不确定你所说的“正确转发”是什么意思,但希望这个例子能做到:

    $host = 'stackoverflow.com';
    $ports = array(21, 25, 80, 81, 110, 443, 3306);
    
    foreach ($ports as $port)
    {
        $connection = @fsockopen($host, $port);
    
        if (is_resource($connection))
        {
            echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";
    
            fclose($connection);
        }
    
        else
        {
            echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";
        }
    }
    

    输出:

    stackoverflow.com:21 is not responding.
    stackoverflow.com:25 is not responding.
    stackoverflow.com:80 (http) is open.
    stackoverflow.com:81 is not responding.
    stackoverflow.com:110 is not responding.
    stackoverflow.com:443 is not responding.
    stackoverflow.com:3306 is not responding.
    

    http://www.iana.org/assignments/port-numbers 获取端口号的完整列表。