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:
- Create a socket on a specified port using socket_create_listen.
- Wait for incoming connections using socket_accept.
- Read data from the socket using socket_read.
- 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.
- Bind to a socket using socket_create.
- Using the socket that was just created, connect to the server’s socket using socket_connect.
- Send a message to the server using socket_send.
- 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.
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.
10 replies on “PHP Dark Arts: Sockets”
[…] This post was mentioned on Twitter by Hacker News YC, Web Startup Group, Tech & Freak Feeds, pok rap, bartezzini and others. bartezzini said: PHP Dark Arts: Sockets: Comments http://goo.gl/fb/Y8BX6 […]
Pretty cool little example. Thank you.
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.
Thanks for the examples provided, I did’nt know where to start with sockets 😉
I liek these sockets, thanks so much for showing dark arts!
Where is the dark side of the moon
[…] Sockets […]