Socket server in PHP

Posted on March 29, 2010
by

A socket UK plug

Recently I had to develop a chat room system very quickly. I absolutely wanted to use my favorite language: PHP. So I had to figure out how do sockets work in PHP, and how to give PHP a daemon behavior (read: Give PHP a daemon behavior).

I found many resources on the Internet, more or less exact, more or less commented… All the articles I read were going around the same solution, and actually by having read some other articles about sockets in C and Java afterwards, it’s indeed the same logic for the 3 languages.

I won’t explain the principles of sockets, nor how to implement them in PHP, it’s already fully documented on the Net (see at the bottom of the article). However, as I wrote a class to help me creating socket servers in PHP, I’m going to share it and explain how to use it.

Step 1: download the class file

Download the Socket Server class: socket-server.zip

Step 2: extend ClsSocketServer

class MyChatServer extends ClsSocketServer { }

Step 3: instantiate and run the start() method

$chatServer = new MyChatServer('localhost',9999,20);
$chatServer->start();
You’re done. The arguments for the constructor are easily guessable: the host on which your socket server will run, the port it will use and the maximum number of connected clients allowed.

Step 4: give your server the behavior you like

Now you have a socket server, you want to give it the behavior you like (making a chat room server, which is the easiest thing to do, or implementing a game server which is more tricky… the only limit being your imagination). These are the methods you can overload:

__construct

Don’t forget to call parent::_construct() when you overload the constructor! Otherwise the server won’t even start. /**
* Constructor
* @param string $address
* @param int $port
* @param int $maxClients
* @return ClsSocketServer
*/
public function __construct($address,$port,$maxClients);

onClientConnected

This method is called when a new client connects to your server. $socket is the resource link to the client’s socket (each client has one resource link) /**
* Method called after a new client is connected
* @param resource $socket
*/
abstract protected function onClientConnected($socket);

onClientDisconnected

This method is called when a client disconnects from your server (he closes the connection). You can also disconnect him in your script with the disconnect($socket) method. $socket is the resource link to the client’s socket. /**
* Method called after a new client is disconnected
* @param resource $socket
*/
abstract protected function onClientDisconnected($socket);

onDataReceived

This method is called when a client sends some data. $socket is the resource link of that client, $data is the string that the client sent. /**
* Method called after a value had been read
* @abstract
* @param resource $socket
* @param string $data
*/
abstract protected function onDataReceived($socket,$data);
The following methods are the methods you can use in your class, but you shouldn’t overload them:

send

Call this method to send data to a client. $data is the string you want to send to him. /**
* Send data to a client
* @param resource $client
* @param string $data
* @return bool
*/
protected function send($client,$data);

sendBroadcast

This method will send the string $data to all the connected clients. /**
* Send data to everybody
* @param string $data
* @return bool
*/
protected function sendBroadcast($data);

disconnect

This method will disconnect a client /**
* Disconnect a client
* @param resource $client
* @return bool
*/
protected function disconnect($client);

start

This method will start the server /**
* Start the server
*/
public function start();

stop

This method will stop the server. All the connected clients will be disconnected. /**
* Stop the server: disconnect all the coonected clients, close the master socket
*/
public function stop();
Et voil√†! Lots of things can be improved, and features can be added, but this might be a good beginning of framework that will fit to many socket-based servers in PHP. I’m waiting for your comments and ideas if any.

References

About the author

Cyril Mazur is a serial web entrepreneur with experience in various fields: online dating, forex & finance, blogging, online advertising... who enjoys building things that people like to use.

5 comments