首页 >函数列表 >socket_create_pair

socket_create_pair

socket_create_pair

(PHP 4 >= 4.1.0, PHP 5)

socket_create_pairCreates a pair of indistinguishable sockets and stores them in an array

说明

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

socket_create_pair() creates two connected and indistinguishable sockets, and stores them in fd. This function is commonly used in IPC (InterProcess Communication).

参数

domain

The domain parameter specifies the protocol family to be used by the socket. See socket_create() for the full list.

type

The type parameter selects the type of communication to be used by the socket. See socket_create() for the full list.

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.

See socket_create() for the full list of supported protocols.

fd

Reference to an array in which the two socket resources will be inserted.

返回值

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

更新日志

版本 说明
5.3.0 This function is now re-enabled on Windows platforms.
4.3.0 This function was due to a bug made unavailable on Windows platforms.

范例

Example #1 socket_create_pair() example

<?php
$sockets 
= array();


$domain = (strtoupper(substr(PHP_OS03)) == 'WIN' AF_INET AF_UNIX);


if (socket_create_pair($domainSOCK_STREAM0$sockets) === false) {
    echo 
"socket_create_pair failed. Reason: ".socket_strerror(socket_last_error());
}

if (socket_write($sockets[0], "ABCdef123 "strlen("ABCdef123 ")) === false) {
    echo 
"socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0]));
}
if ((
$data socket_read($sockets[1], strlen("ABCdef123 "), PHP_BINARY_READ)) === false) {
    echo 
"socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1]));
}
var_dump($data);


socket_close($sockets[0]);
socket_close($sockets[1]);
?>

Example #2 socket_create_pair() IPC example

<?php
$ary 
= array();
$strone 'Message From Parent.';
$strtwo 'Message From Child.';

if (
socket_create_pair(AF_UNIXSOCK_STREAM0$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) {
    

    
socket_close($ary[0]);
    if (
socket_write($ary[1], $stronestrlen($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 ";
    }
    
socket_close($ary[1]);
} else {
    

    
socket_close($ary[1]);
    if (
socket_write($ary[0], $strtwostrlen($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 ";
    }
    
socket_close($ary[0]);
}
?>

参见


  • socket_accept
  • socket_bind
  • socket_clear_error
  • socket_close
  • socket_connect
  • socket_create
  • socket_create_listen
  • socket_create_pair
  • socket_getpeername
  • socket_getsockname
  • socket_get_option
  • socket_last_error
  • socket_listen
  • socket_read
  • socket_recv
  • socket_recvfrom
  • socket_select
  • socket_send
  • socket_sendto
  • socket_set_block
  • socket_set_nonblock
  • socket_set_option
  • socket_shutdown
  • socket_strerror
  • socket_write
  • PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道