在PHP中,如何检查TCP端口是否可用?

发布于 2025-01-22 03:14:32 字数 62 浏览 1 评论 0原文

如何检查PHP中是否有TCP端口?

(我看到这个问题要求其他许多语言,但没有找到PHP的语言)

how do i check if a tcp port is available in PHP?

(i saw this question asked for a lot of other languages, but didn't find one for PHP)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2025-01-29 03:14:32

测试不佳,但我认为这是类似的

function is_tcp_port_available(int $port, int &$error_code = null, string &$error_description = null):bool{
    $sock = socket_create(AF_INET,SOCK_STREAM, SOL_TCP );
    if(!$sock){
        $error_code = socket_last_error();
        $error_description = socket_strerror($error_code);
        return false;
    }
    // use @ to be compatible with ErrorException
    if(!@socket_bind($sock, "0.0.0.0", $port)){
        $error_code = socket_last_error($sock);
        $error_description = socket_strerror($error_code);        
        socket_close($sock);
        return false;
    }
    $error_code = 0;
    $error_description = "";
    socket_close($sock);
    return true;
}

not well tested, but i think it's something like

function is_tcp_port_available(int $port, int &$error_code = null, string &$error_description = null):bool{
    $sock = socket_create(AF_INET,SOCK_STREAM, SOL_TCP );
    if(!$sock){
        $error_code = socket_last_error();
        $error_description = socket_strerror($error_code);
        return false;
    }
    // use @ to be compatible with ErrorException
    if(!@socket_bind($sock, "0.0.0.0", $port)){
        $error_code = socket_last_error($sock);
        $error_description = socket_strerror($error_code);        
        socket_close($sock);
        return false;
    }
    $error_code = 0;
    $error_description = "";
    socket_close($sock);
    return true;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文