我想問一些問題,如果在60秒內讀取或達到我設置的限制,我如何在socket_read中超時。我在如何實現這個問題上存在問題。 我知道這是重複的,但我沒有得到第一線的幫助,這就是爲什麼我要求一些幫助。如何在socket_read中設置超時
預先感謝您。
<?php
$time_limit = 60;
set_time_limit (65);
if(isset($_GET['comm'])){
$command = $_GET['comm'];
$host = "xxx.xx.xxx.xx";
$port = xxxx;
$start_time = time();
$message = $command;
$socket = socket_create(AF_INET, SOCK_STREAM,0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Couldn't connect to server: [$errorcode] $errormsg");
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 60, "usec" => 0));
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// This line I want to put time out if it reaches 60 seconds and
// prompt it to user or show some echo "request time out".
$resultserv = socket_read ($socket, 1024) or die("Could not read server response:
[$errorcode] $errormsg");
echo $resultserv;
socket_close($socket);
}
?>
更新:我添加socket_set_option();如果讀取次數達到60秒,仍然會在socket_read中發生致命錯誤。
http://php.net/manual/en/function.socket-set-option.php – Prix 2014-09-24 02:54:45