这是一些示例代码:
<?php
$fp = fsockopen($host, $port, $errno, $errstr, $connectTimeout);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
echo "connected\n";
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
Run Code Online (Sandbox Code Playgroud)
我见过
stream_set_timeout($fp, 5);和
socket_set_option($fp, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>5, "usec"=>0));,但读永不超时.
我在PHP文档中看到了几个警告stream_set_timeout():
此函数不适用于像stream_socket_recvfrom()这样的高级操作,而是使用带有超时参数的stream_select().
我宁愿不使用select()或循环.使用超时阻塞读取的规范方法是什么?
| 归档时间: |
|
| 查看次数: |
731 次 |
| 最近记录: |