To whom it may concern, and it may concern you greatly, stream_set_blocking has no effect on stream_socket_accept.
If you want it to return right away, connection or not, use 0 for the timeout parameter.
stream_socket_accept
(PHP 5)
stream_socket_accept — Accept a connection on a socket created by stream_socket_server()
Opis
Accept a connection on a socket previously created by stream_socket_server().
Parametry
- timeout
-
Override the default socket accept timeout. Time should be given in seconds.
- peername
-
Will be set to the name (address) of the client which connected, if included and available from the selected transport.
Informacja: Can also be determined later using stream_socket_get_name().
Zwracane wartości
Zwraca TRUE w przypadku powodzenia, FALSE w przypadku błędu.
Notatki
This function should not be used with UDP server sockets. Instead, use stream_socket_recvfrom() and stream_socket_sendto().
Zobacz też:
- stream_socket_server() - Create an Internet or Unix domain server socket
- stream_socket_get_name() - Retrieve the name of the local or remote sockets
- stream_set_blocking() - Set blocking/non-blocking mode on a stream
- stream_set_timeout() - Set timeout period on a stream
- fgets() - Pobiera linię ze wskaźnika pliku
- fgetss() - Pobiera linię ze wskaźnika pliku i usuwa tagi HTML
- fwrite() - Zapis pliku binarnie bezpieczny
- fclose() - Zamyka otwarty wskaźnik pliku
- feof() - Sprawdza czy wskaźnik pliku jest na końcu pliku (EOF)
- cURL Funkcje
stream_socket_accept
02-Jan-2008 08:33
18-Jul-2006 10:10
this function, compared to the function socket_accept, got an extra argument "timeout".
To make this function wait indefinitelly to incoming connections, just as in socket_accept, set timeout to -1. It works for me with PHP 5.0.4.
02-Nov-2004 01:58
This code could be very helpfull...
The following code is for the "server". It listen for a message until CTRL-C
<?php
while (true)
{
// disconnected every 5 seconds...
receive_message('127.0.0.1','85',5);
}
function receive_message($ipServer,$portNumber,$nbSecondsIdle)
{
// creating the socket...
$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);
if (!$socket)
{
echo "$errstr ($errno)<br />\n";
}
else
{
// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.
while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))
{
$message= fread($conn, 1024);
echo 'I have received that : '.$message;
fputs ($conn, "OK\n");
fclose ($conn);
}
fclose($socket);
}
}
?>
The following code is for the "client". It send a message, and read the respons...
<?php
send_message('127.0.0.1','85','Message to send...');
function send_message($ipServer,$portServer,$message)
{
$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);
if (!$fp)
{
echo "ERREUR : $errno - $errstr<br />\n";
}
else
{
fwrite($fp,"$message\n");
$response = fread($fp, 4);
if ($response != "OK\n")
{echo 'The command couldn\'t be executed...\ncause :'.$response;}
else
{echo 'Execution successfull...';}
fclose($fp);
}
}
?>
