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:

  • There is a bug in stream function,
  • fread has the limitation length of 8129, can't support big UDP packet
  • swoole_client supports waitall, able to read all the data if the packet length is known.
  • swoole_client supports UDP connect, there will be no UDP packets mixing up issues.
  • swoole_client has better performance.
  • swoole_client supports async non-blocking callback, along with sync blocking and select API.

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.