php socket 学习
socket超时设置
ini_set("default_socket_timeout", -1);
stream_set_timeout
$fp = fsockopen("www.example.com", 80); if (!$fp) { echo "Unable to open\n"; } else { fwrite($fp, "GET / HTTP/1.0\r\n\r\n"); stream_set_timeout($fp, 2); $res = fread($fp, 2000); $info = stream_get_meta_data($fp); fclose($fp); if ($info['timed_out']) { echo 'Connection timed out!'; } else { echo $res; } }
注意:
1、This function doesn't work with advanced operations like stream_socket_recvfrom(), use stream_select() with timeout parameter instead.