Categories
PHP Programming

PHP Dark Arts: Sockets

Note:  Complete example code for this article is available here.

As an Internet user, you take part in many client-server relationships on a day to day basis.  Most of these relationships are abstracted away, but what if you wanted to make your own client?  Or your own server?  Well, if that’s the case then you’ll probably need to know some network programming.  What you’re really going to need is sockets.

A socket is a receptacle that provides a means of communication between two processes (or in this case, two computers).  Basically it allows you to accept or send information on any port you please (so long as they aren’t reserved or already in use).  So how are we going to use sockets?  Keep reading to find out.

The Server

There are a ton of different examples I could do for this, but I’m choosing to keep it simple.  The server is going to do the following:

  1. Create a socket on a specified port using socket_create_listen.
  2. Wait for incoming connections using socket_accept.
  3. Read data from the socket using socket_read.
  4. Echo the data, and then close the socket using socket_close.

Basically this amounts to creating a server, fetching data from a client, and dieing.  The code itself is very simple, so take a look.

//Set up some variables
$maxBytes = 5000;
$port = 33333;
 
//Create the socket on the specified port.
$socket = socket_create_listen($port);
 
//Wait for incoming connections.
$connection = socket_accept($socket);
print "Connection accepted\n";
 
//When a connection has been accepted, read up to $maxBytes
//then print the received message.
$bytes = socket_read($connection, $maxBytes);
echo "Message From Client: $bytes \n";
 
//Close the socket
socket_close($socket);

Notice that I set the port that I want the socket to bind on very high.  This generally a good idea, because lower ports are regularly used by other applications.  I also have the maximum number of bytes to read set fairly high.  I did this to simplify the example.

The Client

The client for this example is just as simple as the server.  It’s going to do the following.

  1. Bind to a socket using socket_create.
  2. Using the socket that was just created, connect to the server’s socket using socket_connect.
  3. Send a message to the server using socket_send.
  4. Close the connection using socket_close.
//Create a socket and connect it to the server.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, 'localhost', 33333);
 
//Create a message, and send it to the server on $socket.
$message = "This is a message from the client.\n";
socket_send($socket, $message, strlen($message), MSG_EOF);
//Close the socket.
socket_close($socket);

Running The Client & Server

Running the code is easy.  Save your server code to socket_server.php and save your client code to socket_client.php.  After that, open two terminal windows.  In one window run php socket_server.php and in the other run php socket_client.php.  Your output should look like the following image.

PHP Sockets

Please ignore the library error, that’s actually unrelated to this example.  Look at the line above it.

Note:  Complete example code for this article is available here.

Did You Like This Article?

You’ll probably like these other articles from the Dark Arts series as well.

By Jack Slingerland

Founder of Kernl.us. Working and living in Raleigh, NC. I manage teams of software engineers and work in Python, Django, TypeScript, Node.js, React+Redux, Angular, and PHP. I enjoy hanging out with my wife and kids, lifting weights, and PC gaming in my free time.

10 replies on “PHP Dark Arts: Sockets”

Isn’t it a lot faster to build the ‘server side’ socket connection in a language like C? If you’re really going to need this in a production environment that’s about performance and the server side needs to do something like a MySQL insert and/or some calculation&memory storage.. then I’m not sure if PHP is the best language to handle it.

I completely agree. The point of the “Dark Arts” series to show people that PHP is so much more than just a web development platform. Yes, C (or Python, or Java, or Go) would have been a better choice, but then it wouldn’t have been nearly as much fun 🙂

This is pretty useless. No multithreaded server??

As an academic example is ok. Not for use in real life.

Clearly this isn’t for use in real life. Using PHP to write a server isn’t a smart move in real life. Obviously spawning a new thread for each incoming connection would be the right way to do it, but it would also introduce unnecessary complexity. This example is about the basics of using sockets, not about writing a multi-threaded server.

Comments are closed.