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

socket_send

Description

The socket_send of Sockets for PHP sends data to a connected socket.

Syntax

socket_send(
    Socket $socket,
    string $data,
    int $length,
    int $flags
): int|false

Parameters

socket

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

data

A buffer containing the data that will be sent to the remote host.

length

The number of bytes that will be sent to the remote host from data.

flags

The value of flags can be any combination of the following flags, joined with the binary OR (|) operator.

ConstantDescription
MSG_OOBSend out-of-band data.
MSG_EORIndicate a record mark. The sent data completes the record.
MSG_EOFClose the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction.
MSG_DONTROUTEBypass routing, use direct interface.

Return

Returns the number of bytes sent, or false on error.

Examples

1 · socket data length flags · MSG_OOB

<?

$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 = 2048;
            $flags = MSG_OOB;

            $return = socket_send($socket, $data, $length, $flags);

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

                $socket_strerror = socket_strerror($error_code);

                die("socket_send: $socket_strerror");
            }

            var_export($return);

        socket_close($socket);

    socket_close($clientsocket);

socket_close($serversocket);

?>
4

2 · socket data length flags · MSG_EOR

<?

$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 = 2048;
            $flags = MSG_EOR;

            $return = socket_send($socket, $data, $length, $flags);

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

                $socket_strerror = socket_strerror($error_code);

                die("socket_send: $socket_strerror");
            }

            var_export($return);

        socket_close($socket);

    socket_close($clientsocket);

socket_close($serversocket);

?>
4

3 · socket data length flags · MSG_EOF

<?

$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 = 2048;
            $flags = MSG_EOF;

            $return = socket_send($socket, $data, $length, $flags);

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

                $socket_strerror = socket_strerror($error_code);

                die("socket_send: $socket_strerror");
            }

            var_export($return);

        socket_close($socket);

    socket_close($clientsocket);

socket_close($serversocket);

?>
4

4 · socket data length flags · MSG_DONTROUTE

<?

$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 = 2048;
            $flags = MSG_DONTROUTE;

            $return = socket_send($socket, $data, $length, $flags);

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

                $socket_strerror = socket_strerror($error_code);

                die("socket_send: $socket_strerror");
            }

            var_export($return);

        socket_close($socket);

    socket_close($clientsocket);

socket_close($serversocket);

?>
4
HomeMenu