Swoole Client for TCP/UDP

Swoole client provide the wrapper of TCP/UDP sockets client side API.

Compare with PHP stream functions, swoole_client is more advanced:

Table of Contents


A simple sync TCP client:

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

A simple async TCP client:

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) {
    $cli->send("GET / HTTP/1.1\r\n\r\n");
$client->on("receive", function(swoole_client $cli, $data){
    echo "Receive: $data";
    $cli->send(str_repeat('A', 100)."\n");
$client->on("error", function(swoole_client $cli){
    echo "error\n";
$client->on("close", function(swoole_client $cli){
    echo "Connection close\n";
$client->connect('', 9501);

Note, the async TCP client can not be used in PHP-FPM or Apache mode, only can be used in Swoole server.