I'm using PHP Websocket and sometimes, I get the following both warnings.
Warning-1:
socket_recv(): An existing connection was forcibly closed by the remote host
Warning-2:
socket_getpeername(): unable to retrieve peer name [107]: Transport endpoint is not connected
I send messages to the Websocket by another PHP-Script. This PHP-Script sends a message and if it's finised, the Socket-Call ends.
So this Warning is probably correct, because the "endpoint" (= the PHP-Script) is no loger connected.
But this Warning is not beautiful..
So this is the following Code for Websocket, which is received the messages:
define('HOST_NAME',$host_ip);
define('PORT',$socket_port);
$null = NULL;
$socketHandler = new SocketHandler();
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socketResource, 0, PORT);
socket_listen($socketResource);
$clientSocketArray = array($socketResource);
while (true) {
$newSocketArray = $clientSocketArray;
socket_select($newSocketArray, $null, $null, 0, 10);
if (in_array($socketResource, $newSocketArray)) {
$newSocket = socket_accept($socketResource);
$clientSocketArray[] = $newSocket;
$header = socket_read($newSocket, 1024);
$socketHandler->doHandshake($header, $newSocket, HOST_NAME, PORT);
socket_getpeername($newSocket, $client_ip_address);
$newSocketIndex = array_search($socketResource, $newSocketArray);
unset($newSocketArray[$newSocketIndex]);
}
foreach ($newSocketArray as $newSocketArrayResource) {
while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1){
$socketMessage = $socketHandler->unseal($socketData);
$messageObj = json_decode($socketMessage);
break 2;
}
$socketData = @socket_read($newSocketArrayResource, 1024, PHP_NORMAL_READ);
if ($socketData === false) {
socket_getpeername($newSocketArrayResource, $client_ip_address);
$newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
unset($clientSocketArray[$newSocketIndex]);
}
}
}
socket_close($socketResource);
The command, which produces the Warning-1
socket_recv(): An existing connection was forcibly closed by the remote host
is:
while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1)
In the PHP-Manual there is mentioned to get the return-value of socket_recv(). But in the examples there is only shown to execute socket_recv() once, by using if().
But I use socket_recv() within the loop-header.
So my question:
How do I check return-value in my case - by using socket_recv() within a loop-header?
Warning-2
socket_getpeername(): unable to retrieve peer name [107]: Transport endpoint is not connected
was produced with the following code:
socket_getpeername($newSocketArrayResource, $client_ip_address);
My question is:
What have I to do in order not to execute socket_getpeername(), if the client is no longer connected?
Maybe someone can help me to fix these Warnings. It's very difficult do watch, because these Warings do not always appear..
Bye, Chris