Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

socket_read

Description

Reads a maximum of length bytes from a socket

Syntax

socket_read(
    Socket $socket,
    int $length,
    int $mode = PHP_BINARY_READ
): string|false

Parameters

socket

A Socket instance created with socket_create() or socket_accept().

length

The maximum number of bytes read is specified by the length parameter. Otherwise you can use \r, \n, or \0 to end reading (depending on the mode parameter, see below).

mode

A named constant.

ConstantDescription
PHP_BINARY_READUse the system recv() function. Safe for reading binary data.
PHP_NORMAL_READReading stops at \n or \r.

Return

Returns the data as a string on success, or false on error (including if the remote host has closed the connection).

The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual representation of the error.

NOTE: socket_read() returns a zero length string ("") when there is no more data to read.

Examples

1 · socket length

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5000;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        if($clientsocket === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = "data\r\n";
            $data .= "data\r\n";
            $data .= "data\r\n";

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

        socket_close($socket);

        $length = 2048;

        $return = socket_read($clientsocket, $length);

        if($return === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        var_export($return);

    socket_close($clientsocket);

socket_close($serversocket);

?>
'data
data
data
'

2 · mode · PHP_BINARY_READ

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5001;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        if($clientsocket === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = "data\r\n";
            $data .= "data\r\n";
            $data .= "data\r\n";

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

        socket_close($socket);

        $length = 2048;
        $mode = PHP_BINARY_READ;

        $return = socket_read($clientsocket, $length, $mode);

        if($return === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        var_export($return);

    socket_close($clientsocket);

socket_close($serversocket);

?>
'data
data
data
'

3 · mode · PHP_NORMAL_READ

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5002;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = "data\r\n";
            $data .= "data\r\n";
            $data .= "data\r\n";

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

        socket_close($socket);

        $length = 2048;
        $mode = PHP_NORMAL_READ;

        $return = socket_read($clientsocket, $length, $mode);

        if($return === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        var_export($return);

    socket_close($clientsocket);

socket_close($serversocket);

?>
'data
'
HomeMenu