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

socket_write

Description

Write to a socket

Syntax

socket_write(
    Socket $socket,
    string $data,
    ?int $length = null
): int|false

Parameters

socket

data

The buffer to be written.

length

An alternate length of bytes written to the socket. If this length is greater than the buffer length, it is silently truncated to the length of the buffer.

Return

Returns the number of bytes successfully written to the socket or false on failure.

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

NOTE: It is perfectly valid for socket_write() to return zero which means no bytes have been written. Be sure to use the === operator to check for false in case of an error.

Examples

1 · socket data

<?

$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';

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

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

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

            var_export($return);

        socket_close($socket);

    socket_close($clientsocket);

socket_close($serversocket);

?>
4

2 · 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';
            $length = 2;

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

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

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

            var_export($return);

        socket_close($socket);

    socket_close($clientsocket);

socket_close($serversocket);

?>
2
HomeMenu