stream_socket_accept

(PHP 5)

stream_socket_accept — Accept a connection on a socket created by stream_socket_server()

Description

resource stream_socket_accept ( resource $server_socket [, float $timeout [, string &$peername ]] )

Accept a connection on a socket previously created by stream_socket_server(). If timeout is specified, the default socket accept timeout will be overridden with the time specified in seconds. The name (address) of the client which connected will be passed back in peername if included and available from the selected transport.

peername can also be determined later using stream_socket_get_name().

If the call fails, it will return FALSE.

Внимание

This function should not be used with UDP server sockets. Instead, use stream_socket_recvfrom() and stream_socket_sendto().

See also stream_socket_server(), stream_socket_get_name(), stream_set_blocking(), stream_set_timeout(), fgets(), fgetss(), fwrite(), fclose(), feof(), and the Curl extension.

Коментарии

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))
    {
     
$messagefread($conn1024);
     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($fp4);
     if (
$response != "OK\n")
        {echo 
'The command couldn\'t be executed...\ncause :'.$response;}
     else
        {echo 
'Execution successfull...';}
     
fclose($fp);
  }
}
?>
2004-11-02 07:58:23
http://php5.kiev.ua/manual/ru/function.stream-socket-accept.html
To check if there's a new connection waiting, without blocking, or (when using non-blocking mode) without notices), you can use stream_accept (as opposed to socket_select).

<?php

   
class GenericClass {

        protected 
$resSocket=null;

        function 
acceptConnections() {

           
# check that we still have a resource 
           
           
if(is_resource($this->resSocket)) {
           
               
$arrRead=array($this->resSocket);
               
               
$arrWrite=array();
               
               
/** @warning Passing $arrRead,$arrWrite by reference */
               
if(stream_select($arrRead,$arrWrite,$arrWrite,0)) {
           
                   
$resConnection=stream_socket_accept($this->resSocket,0);

                   
# ... other stuff here
               
}
            }
        }
    }
?>
2011-10-10 12:13:36
http://php5.kiev.ua/manual/ru/function.stream-socket-accept.html

    Поддержать сайт на родительском проекте КГБ