Back to the module list

Create a TCP server

Create a TCP server, wait for client connections. Allow to have a SSL encryption if needed. The server is waiting on a port. When a client is connected to the port, run the provided handler.

Each connection is fork in an other process to allow the server to get a new request immediately.

When the handler is finished, the child process is killed.

The default number of childs (active connections) is 500.

Initialize the socket on port 5000 on all the interfaces

public function handler ($tcpserver)
  // This handler will manage the connection if a client is connected on
  // port 5000
  print_r ($tcpserver->getInfo ());
  $read = $tcpserver->read ();
  $tcpserver->send ("Hi\r\n");
  $tcpserver->disconnect ();

$tcpserver = new \tcpserver ();
$tcpserver->init ("::", 5000, array ($this, "handler"));
$tcpserver->loop ();

Activate the SSL support

$options = array (
  "cafile" => "$caCertDir/CA.crt",
  "local_cert" => "$cacheKeysDir/$host.crt",
  "local_pk" => "$cacheKeysDir/$host.key",
  "verify_peer" => false,
$tcpserver->setSSLOptions ($options);
$tcpserver->cryptoEnable (true);

Read data

By default, the server is waiting for text data. It read one line and finished on \r or \n or \r\n. It is possible to put the server in binary mode. Then, the read method will read the maxNumber of bytes.

$tcpserver->readMode ("text");
$read = $tcpserver->read ();
$tcpserver->readMode ("binary");
$read = $tcpserver->read (10);

The class definition

Class \tcpserver

Namespace \


 This class allow to start a TCP server and call a function each time a
 client is connected on it. Each client is separated in a child, so the
 server allow to have multiple simultaneous connections.

 The handler method or function will get one parameter : the client from
 socket_accept. It will allow to use socket_getpeername to get the peer
 address and port, socket_read to get the data from the client,
 socket_write to write on the client, socket_shutdown ($client) and
  socket_close ($client) to finish the connection

 The server has a child limit set to 500 connections by default


No property available


public function cryptoEnable ( $val, $cryptoMethod=8)
 Activate the SSL connection
 Put the socket in blocking mode, as it is mandatory to have SSL connection
 @param boolean $val True to activate, false to disable SSL
 @param integer $cryptoMethod The cryptoMethod allowed

public function disconnect ()
 Disconnect the socket

public function getInfo ()
 Get an array with the peer address, peer port, local address and local
 @return array array ("peer address", peer port, "local address", local

public function getSock ()
 In child, get the socket to direct access
 @return resource The socket with the client

public function init ( $address, $port, $handler)
 Set the address, port and handler that will be enabled by loop
 @param string $address The server address (can be for all IPv4
 interfaces or :: for all the IPv4 and IPv6 interfaces)
 @param integer $port The port to listen
 @param callable $handler The handler that will be called when a client is
 connected to the address:port

public function loop ()
 Start the main loop after the init

public function maxChild ( $val=null)
 Set/get the max children, the maximum of concurrent connections
 @param integer|null $val The number of child to get/set

public function read ( $maxLength=1024)
 Read the data from the client.
 The connection must be established
 Use the readMode in text or binary (text by default)
 In text mode, the read return when found the first \r or the first \n.
 @param integer $maxLength Limit the length of the data from the server
 @return The content

public function readMode ( $readMode=null)
 Set/get the read mode : text or binary
 @param string|null $readMode The mode to set (or get if null)

public function send ( $data)
 Send data to the client
 @param mixed $data The data to send
 @return the length of data sent

public function setSSLOptions ( $options)
 Set context SSL option.
 @param array $options The ssl array to set