TCP/UDP Client

4.x is outdated, please check the latest version 22.x

Latest version: pecl install openswoole-22.1.2

It is a wrapper of Linux TCP/UDP sockets client side API.

The client is blocking and sync can be used within PHP-FPM or simple client side CLI. It should not be used within coroutine context at server side.

TCP/UDP Client supports encrypted UDP DTLS.




When the call of method connect/send/recv/close failed, the value of Swoole\Client->errCode is updated.

The value of Swoole\Client->errCode equals to the value of errno of Linux.

You can use socket_strerror to check the details of an errCode.

echo socket_strerror($client->errCode);


It is an integer which stands for the file descriptor of socket.

You can use this value to convert a stream socket used for fread/fwrite/fclose functions.

$sock = fopen("php://fd/".$swoole_client->sock);


If reuse the socket.

if ($client->reuse) {
    // reused socket
} else {
    // new socket


How many times the socket is reused.


The client ID.


The full list of settings of the client.


A simple sync TCP client:

use Swoole\Client;

$client = new Client(SWOOLE_SOCK_TCP);
if (!$client->connect('', 9501, -1)) {
    exit("connect failed. Error: {$client->errCode}\n");
$client->send("hello world\n");
echo $client->recv();
Last updated on August 31, 2022