php实现socket连接:

 

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