Back to the module list

TCP Client

This class allow a TCP connection to be established to a server.

It allow SSL connection to be enable too.

Basic usage

$tcpclient = new \tcpclient ("domframework.fournier38.fr", 80);
$tcpclient->connect ();
list ($address, $port, $localAddress, $localPort) =
  $tcpclient->getInfo ();
$tcpclient->send ("GET / HTTP/1.1\r\n".
  "Host: domframework.fournier38.fr\r\n".
  "User-Agent: DomFramework\r\n".
  "Accept: *"."/*\r\n".
  "\r\n");
$res = "";
while (($read = $tcpclient->read ()) !== "")
  echo $read."\r\n";
$tcpclient->disconnect ();

Add SSL encryption

The SSL encryption need to stream to be in blocked mode.

$tcpclient->cryptoEnable (true);

The class definition

Class \tcpclient

Namespace \

Description

 TCP Client
 Allow to create TCP connections to a server.
 If both IPv6 and IPv4 are allowed by the server, try in IPv6 then back in
 IPv4 if it doesn't works.
 If the name of the server is provided instead of the IP, look for all the
 IP address, manage the CNAME aliases.
 If multiple addresses are available in IPv6 or IPv4, randomize them to allow
 round-robin connections to the server.
 Manage the timeout, send a command, receive a max number of bytes,
 allow SSL trafic with CA verification

Properties

No property available

Methods

public function __construct ( $ipOrName, $port)
 Initialize the object, by setting the name or the IP of the server
 @param string $ipOrName The IP or the name of the server
 @param integer $port The port of the server to connect

public function connect ()
 Initialize the connection to the server
 Return the socket

public function cryptoEnable ( $val, $cryptoMethod=null, $options=array ())
 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|null $cryptoMethod The cryptoMethod allowed
 @param array|null $options Can overload the SSL options if needed
 @return false if the client can not found a encryption method with the
 server

public function disconnect ()
 Disconnect the socket

public function getInfo ()
 Get the connection peer address, peer port and localaddress and localport
 @return array

public function getMeta ()
 Get meta data, like the timeout state, the crypto protocol and ciphers...

public function getSock ()
 Get the socket to direct access
 @return resource The socket with the client

public function preferIPv4 ( $preferIPv4=null)
 Set/get the preferIPv4 property
 @param boolean|null $preferIPv4 The preferIPv4 property to set (or to get
 if null)

public function read ( $maxLength=1024)
 Read the data from the server.
 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\n, and doesn't
 returns the \r\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 a data to the server.
 The connection must be established
 @param mixed $data The data to send

public function timeout ( $timeout=null)
 Set/get the timeout
 @param integer|null $timeout The timeout in seconds