PHP 8.3.4 Released!

stream_socket_pair

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

stream_socket_pair 创建一对完全一样的网络套接字连接流

说明

stream_socket_pair(int $domain, int $type, int $protocol): array|false

stream_socket_pair() 创建一对完全一样的网络套接字连接,这个函数通常会被用在进程间通信(Inter-Process Communication)。

参数

domain

使用的协议族: STREAM_PF_INET, STREAM_PF_INET6 or STREAM_PF_UNIX

type

通信类型: STREAM_SOCK_DGRAMSTREAM_SOCK_RAWSTREAM_SOCK_RDMSTREAM_SOCK_SEQPACKETSTREAM_SOCK_STREAM

protocol

使用的传输协议: STREAM_IPPROTO_ICMPSTREAM_IPPROTO_IPSTREAM_IPPROTO_RAWSTREAM_IPPROTO_TCPSTREAM_IPPROTO_UDP

注意: 关于每个常量的更多细节,请查阅 Stream 常量列表

返回值

成功时将返回一个包含了两个 socket 资源的 array,错误时返回 false

示例

示例 #1 stream_socket_pair() 例子

这个例子展示了 stream_socket_pair() 进程间通信的基本用法。

<?php

$sockets
= stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$pid = pcntl_fork();

if (
$pid == -1) {
die(
'could not fork');

} else if (
$pid) {
/* 父进程 */
fclose($sockets[0]);

fwrite($sockets[1], "child PID: $pid\n");
echo
fgets($sockets[1]);

fclose($sockets[1]);

} else {
/* 子进程 */
fclose($sockets[1]);

fwrite($sockets[0], "message from child\n");
echo
fgets($sockets[0]);

fclose($sockets[0]);
}

?>

以上示例的输出类似于:

child PID: 1378
message from child

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top