天天看点

php实现socket连接

  1. <?php 
  2.     $port = 9050 ; 
  3.     // create a streaming socket, of type TCP/IP 
  4.     $sock = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP ); 
  5.     // set the option to reuse the port 
  6.     socket_set_option ( $sock , SOL_SOCKET , SO_REUSEADDR , 1 ); 
  7.     // "bind" the socket to the address to "localhost", on port $port 
  8.     // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc.. 
  9.     socket_bind ( $sock , 0 , $port ); 
  10.     // start listen for connections 
  11.     socket_listen ( $sock ); 
  12.     // create a list of all the clients that will be connected to us.. 
  13.     // add the listening socket to this list 
  14.     $clients = array( $sock ); 
  15.     while ( true ) { 
  16.         // create a copy, so $clients doesn't get modified by socket_select() 
  17.         $read = $clients ; 
  18.         // get a list of all the clients that have data to be read from 
  19.         // if there are no clients with data, go to next iteration 
  20.         if ( socket_select ( $read , $write = NULL , $except = NULL , 0 ) < 1 ) 
  21.             continue; 
  22.         // check if there is a client trying to connect 
  23.         if ( in_array ( $sock , $read )) { 
  24.             // accept the client, and add him to the $clients array 
  25.             $clients [] = $newsock = socket_accept ( $sock ); 
  26.             // send the client a welcome message 
  27.             socket_write ( $newsock , "no noobs, but ill make an exception :)\n" . 
  28.             "There are " .( count ( $clients ) - 1 ). " client(s) connected to the server\n" ); 
  29.             socket_getpeername ( $newsock , $ip ); 
  30.             echo "New client connected: { $ip } \n" ; 
  31.             // remove the listening socket from the clients-with-data array 
  32.             $key = array_search ( $sock , $read ); 
  33.             unset( $read [ $key ]); 
  34.         } 
  35.         // loop through all the clients that have data to read from 
  36.         foreach ( $read as $read_sock ) { 
  37.             // read until newline or 1024 bytes 
  38.             // socket_read while show errors when the client is disconnected, so silence the error messages 
  39.             $data = @ socket_read ( $read_sock , 1024 , PHP_NORMAL_READ ); 
  40.             // check if the client is disconnected 
  41.             if ( $data === false ) { 
  42.                 // remove client for $clients array 
  43.                 $key = array_search ( $read_sock , $clients ); 
  44.                 unset( $clients [ $key ]); 
  45.                 echo "client disconnected.\n" ; 
  46.                 // continue to the next client to read from, if any 
  47.                 continue; 
  48.             } 
  49.             // trim off the trailing/beginning white spaces 
  50.             $data = trim ( $data ); 
  51.             // check if there is any data after trimming off the spaces 
  52.             if (!emptyempty( $data )) { 
  53.                 // send this to all the clients in the $clients array (except the first one, which is a listening socket) 
  54.                 foreach ( $clients as $send_sock ) { 
  55.                     // if its the listening sock or the client that we got the message from, go to the next one in the list 
  56.                     if ( $send_sock == $sock || $send_sock == $read_sock ) 
  57.                         continue; 
  58.                     // write the message to the client -- add a newline character to the end of the message 
  59.                     socket_write ( $send_sock , $data . "\n" ); 
  60.                 } // end of broadcast foreach 
  61.             } 
  62.         } // end of reading foreach 
  63.     } 
  64.     // close the listening socket 
  65.     socket_close ( $sock ); 
  66. ?>