0
我一直在嘗試製作一個PHP套接字服務器,這是我以前從未做過的事情。所以我可能不知道所有的socket_ *函數是如何工作的。socket_select永不超時
我遇到的問題是socket_select中的超時函數。
while(true){
//Copy $clients so the list doesn't get modified by socket_select();
$read = $clients;
$write = $clients;
//new socket tries to connect
if(!$new = socket_accept($socket)){
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error()); break;
}
//Accept the new client
if(!in_array($new, $clients)){
$clients[] = $new;
sendMessage($new, "Hello and welcome to the PHP server!");
}
//Wait for read
socket_select($read, $write, $empty, 5, 5);
foreach($read as $client){
$id = array_search($client,$clients);
echo $id." ".readMessage($client);
}
//Write data to the connected sockets
foreach($write as $client){
sendMessage($client, rand(0,99999));
}
echo "I'm bored\n";
}
從我對socket_select的理解中可以看出,這個腳本應該每隔5秒就說「我很無聊」。但它不,爲什麼?
爲什麼我想超時socket_select是做一個循環,所以我可以發送數據到連接的套接字。
非常感謝,而且這一直都是我認爲這是'socket_select',工作。 – Yemto 2015-02-11 05:20:38