• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • socket_create_pair()

    (PHP 4 >= 4.1.0, PHP 5, PHP 7)

    创建一对没有区别的socket到一个数组里

    说明

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

    socket_create_pair()创建两个连接且不可区分的套接字,并将它们存储在$pair.该功能在IPC(InterProcess Communication)中常用。

    参数

    $domain

    该参数指定套接字使用的协议族。

    $type

    该参数选择套接字使用的通信类型。

    $protocol

    在返回的套接字上进行通信时设置要使用的指定协议内的特定协议。可以使用getprotobyname()按名称检索正确的值。如果需要的协议是TCP,或者UDP,对应的常量SOL_TCPSOL_UDP也可以使用。

    $pair

    对将插入两个 Socket 实例的数组的引用

    返回值

    成功时返回TRUE,或者在失败时返回FALSE

    参见

    范例

    <?php
    $sockets = array();
    /* On Windows we need to use AF_INET */
    $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
    
    /* Setup socket pair */
    if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) {
        echo "socket_create_pair failed. Reason: ".socket_strerror(socket_last_error());
    }
    
    /* Send and Recieve Data */
    if (socket_write($sockets[0], "ABCdef123\n", strlen("ABCdef123\n")) === false) {
        echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0]));
    }
    if (($data = socket_read($sockets[1], strlen("ABCdef123\n"), PHP_BINARY_READ)) === false) {
        echo "socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1]));
    }
    var_dump($data);
    
    /* Close sockets */
    socket_close($sockets[0]);
    socket_close($sockets[1]);
    ?>
    

    socket_create_pair() IPC example

    <?php
    $ary = array();
    $strone = 'Message From Parent.';
    $strtwo = 'Message From Child.';
    if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary) === false) {
        echo "socket_create_pair() failed. Reason: ".socket_strerror(socket_last_error());
    }
    $pid = pcntl_fork();
    if ($pid == -1) {
        echo 'Could not fork Process.';
    } elseif ($pid) {
        /*parent*/
        socket_close($ary[0]);
        if (socket_write($ary[1], $strone, strlen($strone)) === false) {
            echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[1]));
        }
        if (socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) {
            echo "Recieved $strtwo\n";
        }
        socket_close($ary[1]);
    } else {
        /*child*/
        socket_close($ary[1]);
        if (socket_write($ary[0], $strtwo, strlen($strtwo)) === false) {
            echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[0]));
        }
        if (socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) {
            echo "Recieved $strone\n";
        }
        socket_close($ary[0]);
    }
    ?>
    
    The underlying sockpair() function does only support AF_UNIX at least on BSD and Linux.
    There is a syntax error in one of the code samples provided, it should look like this:
    <?php
    $sockets = array();
    /* Setup socket pair */
    if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets) === false) {
      echo "socket_create_pair failed. Reason: ".socket_strerror(socket_last_error());
    }
    /* Send and Recieve Data */
    if (socket_write($sockets[0], "ABCdef123\n", strlen("ABCdef123\n")) === false) {
      echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0]));
    }
    if (($data = socket_read($sockets[1], strlen("ABCdef123\n"), PHP_BINARY_READ)) === false) {
      echo "socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1]));
    }
    var_dump($data);
    /* Close sockets */
    socket_close($sockets[0]);
    socket_close($sockets[1]);
    ?>