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

socket_create_pair

Description

The socket_create_pair of Sockets for PHP creates a pair of indistinguishable sockets and stores them in an array.

Syntax

socket_create_pair(
    int $domain,
    int $type,
    int $protocol,
    array &$pair
): bool

Parameters

domain

The domain parameter specifies the protocol family to be used by the socket.

ConstantDescription
AF_INETIPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family.
AF_INET6IPv6 Internet based protocols. TCP and UDP are common protocols of this protocol family.
AF_UNIXLocal communication protocol family. High efficiency and low overhead make it a great form of IPC (Interprocess Communication).

type

The type parameter selects the type of communication to be used by the socket.

ConstantDescription
SOCK_STREAMProvides sequenced, reliable, full-duplex, connection-based byte streams. An out-of-band data transmission mechanism may be supported. The TCP protocol is based on this socket type.
SOCK_DGRAMSupports datagrams (connectionless, unreliable messages of a fixed maximum length). The UDP protocol is based on this socket type.
SOCK_SEQPACKETProvides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each read call.
SOCK_RAWProvides raw network protocol access. This special type of socket can be used to manually construct any type of protocol. A common use for this socket type is to perform ICMP requests (like ping).
SOCK_RDMProvides a reliable datagram layer that does not guarantee ordering. This is most likely not implemented on your operating system.

protocol

The protocol parameter sets the specific protocol within the specified domain to be used when communicating on the returned socket. The proper value can be retrieved by name by using getprotobyname(). If the desired protocol is TCP, or UDP the corresponding constants SOL_TCP, and SOL_UDP can also be used.

ConstantNameDescription
icmpThe Internet Control Message Protocol is used primarily by gateways and hosts to report errors in datagram communication. The "ping" command (present in most modern operating systems) is an example application of ICMP.
SOL_UDPudpThe User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead.
SOL_TCPtcpThe Transmission Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all data packets will be received in the order in which they were sent. If any packet is somehow lost during communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of partial record transmission.

pair

Reference to an array in which the two Socket instances will be inserted.

Return

Returns true on success or false on failure.

Examples

1 · domain type protocol pair

<?

$domain = AF_UNIX;
$type = SOCK_STREAM;
$protocol = 0;

$return = socket_create_pair($domain, $type, $protocol, $pair);

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

        $socket_strerror = socket_strerror($error_code);

        die("socket_create_pair: $socket_strerror");
    }

    var_export($return);

socket_close($pair[0]);
socket_close($pair[1]);
true

2 · socket_write socket_read

<?

$domain = AF_UNIX;
$type = SOCK_STREAM;
$protocol = 0;

$return = socket_create_pair($domain, $type, $protocol, $pair);

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

        $socket_strerror = socket_strerror($error_code);

        die("socket_create_pair: $socket_strerror");
    }

    $data = 'data';

    $socket_write = socket_write($pair[0], $data);

    if($socket_write === false)
    {
        $error_code = socket_last_error($pair[0]);

        $socket_strerror = socket_strerror($error_code);

        die("socket_write: $socket_strerror");
    }

    echo "socket_write: $socket_write\n";

    $length = strlen($data);

    $socket_read = socket_read($pair[1], $length);

    if($socket_read === false)
    {
        $error_code = socket_last_error($pair[1]);

        $socket_strerror = socket_strerror($error_code);

        die("socket_read: $socket_strerror");
    }

    echo "socket_read: $socket_read";

socket_close($pair[0]);
socket_close($pair[1]);
socket_write: 4
socket_read: data

3 · InterProcess Communication

<?

$string1 = 'message from parent';
$string2 = 'message from child';

$domain = AF_UNIX;
$type = SOCK_STREAM;
$protocol = 0;

if(socket_create_pair($domain, $type, $protocol, $pair) === false)
{
    echo "socket_create_pair: ".socket_strerror(socket_last_error());
}

$pid = pcntl_fork();
if($pid == -1)
{
    echo 'could not fork process';
}
elseif($pid)
{
    //parent
    socket_close($pair[0]);
    if(socket_write($pair[1], $string1) === false)
    {
        echo "socket_write: ".socket_strerror(socket_last_error($pair[1]));
    }
    if(socket_read($pair[1], strlen($string2)) == $string2)
    {
        echo "received $string2\n";
    }
    socket_close($pair[1]);
}
else
{
    //child
    socket_close($pair[1]);
    if(socket_write($pair[0], $string2) === false)
    {
        echo "socket_write: ".socket_strerror(socket_last_error($pair[0]));
    }
    if(socket_read($pair[0], strlen($string1)) == $string1)
    {
        echo "received $string1\n";
    }
    socket_close($pair[0]);
}