TCP/UDP Client

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.


  • Swoole\Client->__construct
  • Swoole\Client->set
  • Swoole\Client->connect
  • Swoole\Client->recv
  • Swoole\Client->send
  • Swoole\Client->sendfile
  • Swoole\Client->sendto
  • Swoole\Client->shutdown
  • Swoole\Client->enableSSL
  • Swoole\Client->getPeerCert
  • Swoole\Client->verifyPeerCert
  • Swoole\Client->isConnected
  • Swoole\Client->getsockname
  • Swoole\Client->getpeername
  • Swoole\Client->close
  • Swoole\Client->getSocket



    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();