Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Socket Functions : socket_accept

socket_accept

Accepts a connection on a socket (PHP 4 >= 4.0.7, PHP 5)
resource socket_accept ( resource socket )

Examples ( Source code ) » socket_accept

<?php
$socket 
socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,$address,$port);
socket_listen($socket);
echo 
"Waiting for a connection\n";
$conn false;
switch(@
socket_select($r = array($socket), $w = array($socket), $e = array($socket), 60)) {
   case 
2:
       echo 
"Connection refused\n";
       break;
   case 
1:
       echo 
"Connection accepted\n";
       
$conn = @socket_accept($socket);
       break;
   case 
0:
       echo 
"Connection timed out\n";
       break;
}


if (
$conn !== false) {
   
// communicate over $conn
}

Code Examples / Notes » socket_accept

greg maclellan

The socket returned by this resource will be non-blocking, regardless of what the listening socket is set to. This is actually true for all FCNTL modifiers.

galantonp

socket_accept with timeout, seems to work for me on Apache/1.3.37 (FreeBSD 6.0) PHP/4.4.7.
Adapted from ScriptBlue at nyc dot rr dot com's post under socket_connect.
<?php
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,$address,$port);
socket_listen($socket);
echo "Waiting for a connection\n";
$conn = false;
switch(@socket_select($r = array($socket), $w = array($socket), $e = array($socket), 60)) {
   case 2:
       echo "Connection refused\n";
       break;
   case 1:
       echo "Connection accepted\n";
       $conn = @socket_accept($socket);
       break;
   case 0:
       echo "Connection timed out\n";
       break;
}


if ($conn !== false) {
   // communicate over $conn
}
 
?>


gmkarl

Be aware signal handler functions set with pcntl_signal are not called while a socket is blocking waiting for a connection; the signal is absorbed silently and the handler called when a connection is made.

diogo

Accepting a connection using php-sockets:
$fd = socket_create(AF_INET, SOCK_STREAM, 6 /* OR getprotobyname("TCP")*/);
$PORT = 5000;
socket_bind($fd, "0.0.0.0", $PORT);
while(true)
{
$remote_fd = socket_accept($fd);
remote_socket_client_handle($remote_fd);
}
It is simple!


simon

>Accepting a connection using php-sockets:
>
>$fd = socket_create(AF_INET, SOCK_STREAM, 6 /* OR >getprotobyname("TCP")*/);
>
>$PORT = 5000;
>
>socket_bind($fd, "0.0.0.0", $PORT);
>
>while(true)
>{
>$remote_fd = socket_accept($fd);
>
>remote_socket_client_handle($remote_fd);
>
>}
>
>It is simple!
This example doesn't work. You have to call socket_listen($fd) after your bind in order to accept incoming connections.
Simon


Change Language


Follow Navioo On Twitter
socket_accept
socket_bind
socket_clear_error
socket_close
socket_connect
socket_create_listen
socket_create_pair
socket_create
socket_get_option
socket_getpeername
socket_getsockname
socket_last_error
socket_listen
socket_read
socket_recv
socket_recvfrom
socket_select
socket_send
socket_sendto
socket_set_block
socket_set_nonblock
socket_set_option
socket_shutdown
socket_strerror
socket_write
eXTReMe Tracker