Advertisement
AnotherTest

Server Example v2

Jun 21st, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. <?php
  2. // Indefinite execution
  3. set_time_limit (0);
  4. // IP and port
  5. $strIp = '94.224.24.109';
  6. $intPort = 9000;
  7.  
  8. // Create a TCP Stream socket
  9. $resSock = socket_create(AF_INET, SOCK_STREAM, 0);
  10. // Bind the socket to an address/port
  11. socket_bind($resSock, $strIp, $intPort) or die('Could not bind to address');
  12. // Start listening for connections
  13. socket_listen($resSock);
  14. while(true) {
  15.     // Accept incoming requests
  16.     $resClient = socket_accept($resSock);
  17.     // Read input from client
  18.     $strMessage = socket_read($resClient, 1024);
  19.     sleep(2);
  20.     // Write some stuff to client
  21.     socket_write($resClient, 'Hello...');
  22.     // Display output
  23.     echo $strMessage;
  24. }
  25. // Close the client (child) socket
  26. socket_close($resClient);
  27.  
  28. // Close the master sockets
  29. socket_close($resSock);
  30. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement