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 Domframework\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 Domframework\Tcpserver

Namespace Domframework

Description

/**
 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

Properties

No property available

Methods

public function __construct ()
/**
 The constructor add the error handler needed to catch the error on
 stream_select () when the process is killed

public function __destruct ()
/**
 The destructor is a log for debug

final public function cryptoEnable ( $val, $cryptoMethod=120)
/**
 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

final public function disconnect ()
/**
 Disconnect the socket

public function errorHandler ( $errNo, $errMsg, $file, $line)
/**
 Error catcher.
 By default, do nothing, but can be overrided by the user
 @param integer $errNo The error number
 @param string $errMsg The error message
 @param string $file The file name
 @param integer $line The line where the error raised

public function exceptionHandler ( $exception)
/**
 Exception catcher
 By default do nothing, but can be overrided by the user
 @param object $exception The exception to catch

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

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

final 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 0.0.0.0 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 logDebug ( $params)
/**
 Log the debug, By defaul do nothing, but can be overrided by the user
 @param mixed|null $params The data to store in log

public function logError ( $params)
/**
 Log the errors, By defaul do nothing, but can be overrided by the user
 @param mixed|null $params The data to store in log

public function logMethods ( $method, $args)
/**
 Log the methods called. By default, do nothing, but can be overrided by
 the user
 @param string $method The data to store in log
 @param mixed|null $args The data to store in log

public function logReceive ( $data)
/**
 Log the data received from the client. By default, do nothing, but can be
 overrided by the user
 @param string $data The data to store in log

public function logSend ( $data)
/**
 Log the data send to the client. By default, do nothing, but can be
 overrided by the user
 @param string $data The data to store in log

final public function loop ()
/**
 Start the main loop after the init and keep in it until loopStop

final public function loopInBackgroundStart ()
/**
 Start the main loop in background and do not wait its end
 @return integer the PID of the child

final public function loopInBackgroundStop ()
/**
 Stop the main loop in background and wait until its end

final public function loopStop ()
/**
 Request the loop to stop. Will not allow new connections, but wait the
 end of the existing processus
 Block until all is closed

final 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

final public function processName ( $val=null)
/**
 Set the process name displayed in system
 @param string|null $val The name of the process to set (or get if null)

final 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 string The content

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

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

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

final public function timeout ( $val=null)
/**
 Set the timeout for open communication without any data transmited.
 @param string|null $val The number of seconds to set (or get if null)

public function timeoutHandler ()
/**
 Manage the timeout handler
 By default, disconnect and generate an exception