PHP Socket Programming Posted by: Jordan in ProgrammingPHP on
If you've used PHP for a while your probably use to using PHP to connect to and fetch content from external source.  What about allowing users to connect to your server using PHP?   This may not be the best method (I'd use C or C++) but with  PHP CLI this becomes possible.  

Besides CLI you'll also need the extension.  In Linux/Unix type systems enable the extensions at compile time using --enable-sockets. In Windows uncomment extension=php_sockets.dll in your php.ini file.


Creating a Socket
First you'll need to define some variables such as "host" IP and "port" number.  The port range can be 1-65535 but be careful not to use any common ports already in use by another program.

$host = "127.0.0.1";
$port = 65534;


The next three steps will open the socket, bind the address/port and listen on the open socket.


 
  1. // Create a TCP Stream socket
  2. $sock = socket_create(AF_INET, SOCK_STREAM, 0);
  3.  
  4. // Bind the socket to an address/port
  5. socket_bind($sock, $host, $port) or die('Could not bind to address');
  6.  
  7. // Start listening for connections
  8. socket_listen($sock);


Next we need to accept incoming connection request. 


$client = socket_accept($sock);


So far we have bound the IP/Port and allowed incoming connections but the script doesn't actually do anything.  To start lets send a message to the client upon connection.


$message = "Welcome to the PHP Socket Program!";
$message .= " Enter 'quit' to exit: ";
socket_write($client, $message);


Now that we've sent a message to the user and told them how to exit the program, lets read their input and loop until they type q.


// Read user input and loop until
// it says 'quit'
while (trim(socket_read($client, 1024)) != "q") {
// Display error message
$erMsg = "Not a command! Enter 'q' to exit: ";
socket_write($client, $erMsg);
}


Next (once the user has exited) we will send a goodbye message and close the sockets.

 

// Send goodbye message
$thxMsg = "Thanks for stopping by!";
socket_write($client, $thxMsg);

 

// Close the client (child) socket
socket_close($client);

// Close the master sockets
socket_close($sock);

 


What Next?
Forking will allow you to accept multiple clients at once and even create a simple IRC/chat server.  I won't go into detail on this but you've learned the basics of socket programming now.  It isn't hard and the possibilities are endless.


Complete Script

 
  1. // set some variables
  2. $host = "127.0.0.1";
  3. $port = 65534;
  4.  
  5. // don't timeout!
  6. set_time_limit(0);
  7.  
  8. // Create a TCP Stream socket
  9. $sock = socket_create(AF_INET, SOCK_STREAM, 0);
  10.  
  11. // Bind the socket to an address/port
  12. socket_bind($sock, $host, $port) or die('Could not bind to address');
  13.  
  14. // Start listening for connections
  15. socket_listen($sock);
  16.  
  17. // Accept incoming connections
  18. $client = socket_accept($sock);
  19.  
  20. // Send message to user
  21. $message = "Welcome to the PHP Socket Program!n";
  22. $message .= " Enter 'q' to exit: ";
  23. socket_write($client, $message);
  24.  
  25. // Read user input and loop until
  26. // it says 'quit'
  27. while (trim(socket_read($client, 1024)) != "q") {
  28. // Display error message
  29. $erMsg = "Not a command! Enter 'q' to exit: ";
  30. socket_write($client, $erMsg);
  31. }
  32.  
  33. // Send goodbye message
  34. $thxMsg = "Thanks for stopping by!";
  35. socket_write($client, $thxMsg);
  36.  
  37. // Close the client (child) socket
  38. socket_close($client);
  39.  
  40. // Close the master sockets
  41. socket_close($sock);

 


 


Trackback(0)
feed0 Comments

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy