Advertisement
Guest User

Untitled

a guest
Nov 14th, 2010
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3.  
  4. /* Allow the script to hang around waiting for connections. */
  5. set_time_limit(0);
  6.  
  7. /* Turn on implicit output flushing so we see what we're getting
  8.  * as it comes in. */
  9. ob_implicit_flush();
  10.  
  11. $address = 'xx';
  12. $port = 1;
  13.  
  14. if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
  15.     echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  16. }
  17.  
  18. if (socket_bind($sock, $address, $port) === false) {
  19.     echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
  20. }
  21.  
  22. if (socket_listen($sock, 5) === false) {
  23.     echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
  24. }
  25.  
  26. $clients = array();
  27.  
  28. $read = array();
  29. $write  = NULL;
  30. $except = NULL;
  31.  
  32. $foo = true;
  33. $msg = "";
  34.  
  35. do {
  36.  
  37.     $read[0] = $sock;
  38.        
  39.     //For each active client
  40.     for($i = 0; $i < count($clients); $i++)
  41.     {
  42.         if($clients[$i] != null)    unset($clients[$i]);
  43.         else
  44.             $read[$i+1] = $clients;
  45.     }
  46.  
  47.     //Check for new clients
  48.     $num_changed_sockets = socket_select($read, $write, $except, 0, 1);
  49.    
  50.     // Determine if we already have this client
  51.     if(!in_array($sock,$read))
  52.     {  
  53.         if (($msgsock = socket_accept($sock)) === false) {
  54.             echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
  55.         }  
  56.         else
  57.         {
  58.             $clients[] = $msgsock;
  59.            
  60.             $caddress = "";
  61.             $cport = "";
  62.            
  63.             $client_count = "Client count: ".count($clients);
  64.             socket_getpeername($msgsock,&$caddress,&$cport);
  65.            
  66.             /* Send instructions. */
  67.             $msg .= "\nTCP server connection established \n".$client_count."\n";
  68.             $msg .= "Client connected ".$caddress." {##}\n";
  69.            
  70.             socket_write($msgsock, $msg, strlen($msg));
  71.             echo $msg;
  72.         }
  73.     }
  74.  
  75.     print_r($clients); 
  76.    
  77.     //For each active client
  78.     for($i = 0; $i < count($clients); $i++)
  79.     {
  80.         var_dump($clients[$i]);
  81.         echo "blocking";
  82.  
  83.         if (false === ($input = socket_read($clients[$i], 2048, PHP_BINARY_READ))) {
  84.             echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
  85.         }
  86.        
  87.  
  88.         echo "\ninput says\n";
  89.         echo var_dump($input);
  90.        
  91.         if(empty($input))   continue;
  92.        
  93.        
  94.         // Shutdown the server
  95.         if($input=="shutdown")
  96.         {
  97.             echo "Shutting down..";
  98.             socket_close($clients[$i]);
  99.         }
  100.  
  101.         //Write reponse back to all the clients
  102.         for($j = 0; $j < count($clients); $j++)
  103.         {
  104.         //  socket_write($clients[$j],$input,strlen($input));  
  105.         }
  106.     }
  107.  
  108.  
  109. } while ($foo);
  110.  
  111. socket_close($sock);
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement