开发者

Check if PHP UDP fread is getting a reply/set a timeout

I am creating a website that connects to a windows program on the local network using UDP.

The problem I'm having is that when there is no reply to fread the code has a fatal error when the execution time reaches thirty seconds.

As UDP doesn't check 开发者_开发技巧if it has a connection there seems to be no way to tell what's going on or to catch the thirty seconds fatal error.

Here is my current code.

$fp = fsockopen("udp://1.10.100.28", 24, $errno, $errstr,3);

    if (!$fp) {
        echo "ERROR: $errno - $errstr<br />\n";
    } else {
     $value = $_POST["sendvalue"];
     $out = "setwarpterm 1 1 x ".$value."\r";

     $write = fwrite($fp, $out);

     if($_POST["read"] == "yes"){
         echo fread($fp, 3000);
     }

        fclose($fp);
    }


The timeout didn't work as it wasn't directly above my fread.

stream_set_timeout($fp, 2);
echo fread($fp, 3500);

This did work and makes the fread return an empty string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜