在PHP中检查UDP端口

在PHP中检查UDP端口,php,sockets,udp,Php,Sockets,Udp,我正在尝试使用PHP检查服务器UDP端口。这是我的密码: <?php set_error_handler('my_error_handler'); function my_error_handler($errno, $errstr, $errfile, $errline) {} function checkUDP($host,$port=80){ $fp = fsockopen("udp://".$host, $port, $errno, $errstr,1.0); if

我正在尝试使用PHP检查服务器UDP端口。这是我的密码:

<?php
set_error_handler('my_error_handler');
function my_error_handler($errno, $errstr, $errfile, $errline) {}
function checkUDP($host,$port=80){
    $fp = fsockopen("udp://".$host, $port, $errno, $errstr,1.0);
    if (!$fp) {
        return false;
    } else {
        fclose($fp);
        return true;
    }
}
if(checkUDP($MyIP,9)){
    echo $MyIP.' is open';
}else{
    echo $MyIP.' is closed';
}
echo '<br />';
?> 


但我总是得到真正的结果(端口是开放的)。此代码适用于TCP端口,但为什么我无法获得UDP端口结果?

您是否已经在该端口上运行了服务?UDP端口的工作方式与TCP端口不同。检查打开的UDP端口需要尝试向与该端口关联的特定服务发送数据包,并监视返回的响应。您现有的方法将永远无法工作。请注意,udp是一个无连接协议,套接字打开不会创建任何网络流量。我使用了fwrite,但响应时间太长。你能写一个简单的代码吗?