| PHP Socket Programming Posted by: Jordan in Programming, PHP on Jun 23, 2008 |
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.
The next three steps will open the socket, bind the address/port and listen on the open socket.

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.
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

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;
$port = 65534;
The next three steps will open the socket, bind the address/port and listen on the open socket.

- // Create a TCP Stream socket
- $sock = socket_create(AF_INET, SOCK_STREAM, 0);
- // Bind the socket to an address/port
- socket_bind($sock, $host, $port) or die('Could not bind to address');
- // Start listening for connections
- socket_listen($sock);
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $host, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $host, $port) or die('Could not bind to address');
// Start listening for connections
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);
socket_close($client);
$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

- // set some variables
- $host = "127.0.0.1";
- $port = 65534;
- // don't timeout!
- set_time_limit(0);
- // Create a TCP Stream socket
- $sock = socket_create(AF_INET, SOCK_STREAM, 0);
- // Bind the socket to an address/port
- socket_bind($sock, $host, $port) or die('Could not bind to address');
- // Start listening for connections
- socket_listen($sock);
- // Accept incoming connections
- $client = socket_accept($sock);
- // Send message to user
- $message = "Welcome to the PHP Socket Program!n";
- $message .= " Enter 'q' to exit: ";
- socket_write($client, $message);
- // 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);
- }
- // 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);
// set some variables
$host = "127.0.0.1";
$port = 65534;
// don't timeout!
set_time_limit(0);
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $host, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
// Accept incoming connections
$client = socket_accept($sock);
// Send message to user
$message = "Welcome to the PHP Socket Program!n";
$message .= " Enter 'q' to exit: ";
socket_write($client, $message);
// 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);
}
// 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);
$host = "127.0.0.1";
$port = 65534;
// don't timeout!
set_time_limit(0);
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $host, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
// Accept incoming connections
$client = socket_accept($sock);
// Send message to user
$message = "Welcome to the PHP Socket Program!n";
$message .= " Enter 'q' to exit: ";
socket_write($client, $message);
// 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);
}
// 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);