<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >socket_create_pair</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Socket Functions" HREF="ref.sockets.html"><LINK REL="PREVIOUS" TITLE="socket_create_listen" HREF="function.socket-create-listen.html"><LINK REL="NEXT" TITLE="socket_create" HREF="function.socket-create.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.socket-create-listen.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.socket-create.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.socket-create-pair" ></A >socket_create_pair</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN212926" ></A ><P > (PHP 4 >= 4.1.0, PHP 5)</P >socket_create_pair -- Creates a pair of indistinguishable sockets and stores them in an array</DIV ><DIV CLASS="refsect1" ><A NAME="AEN212929" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >socket_create_pair</B > ( int domain, int type, int protocol, array &fd )<BR ></BR ><P > <B CLASS="function" >socket_create_pair()</B > creates two connected and indistinguishable sockets, and stores them in <CODE CLASS="parameter" >fd</CODE >. This function is commonly used in IPC (InterProcess Communication). </P ><P > The <CODE CLASS="parameter" >domain</CODE > parameter specifies the protocol family to be used by the socket. </P ><DIV CLASS="table" ><A NAME="AEN212951" ></A ><P ><B >表 1. Available address/protocol families</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Domain</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD >AF_INET</TD ><TD > IPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family. Supported only in windows. </TD ></TR ><TR ><TD >AF_INET6</TD ><TD > IPv6 Internet based protocols. TCP and UDP are common protocols of this protocol family. Support added in PHP 5.0.0. Supported only in windows. </TD ></TR ><TR ><TD >AF_UNIX</TD ><TD > Local communication protocol family. High efficiency and low overhead make it a great form of IPC (Interprocess Communication). </TD ></TR ></TBODY ></TABLE ></DIV ><P > The <CODE CLASS="parameter" >type</CODE > parameter selects the type of communication to be used by the socket. </P ><DIV CLASS="table" ><A NAME="AEN212970" ></A ><P ><B >表 2. Available socket types</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Type</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD >SOCK_STREAM</TD ><TD > Provides 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. </TD ></TR ><TR ><TD >SOCK_DGRAM</TD ><TD > Supports datagrams (connectionless, unreliable messages of a fixed maximum length). The UDP protocol is based on this socket type. </TD ></TR ><TR ><TD >SOCK_SEQPACKET</TD ><TD > Provides 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. </TD ></TR ><TR ><TD >SOCK_RAW</TD ><TD > Provides 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, traceroute, etc). </TD ></TR ><TR ><TD >SOCK_RDM</TD ><TD > Provides a reliable datagram layer that does not guarantee ordering. This is most likely not implemented on your operating system. </TD ></TR ></TBODY ></TABLE ></DIV ><P > The <CODE CLASS="parameter" >protocol</CODE > parameter sets the specific protocol within the specified <CODE CLASS="parameter" >domain</CODE > to be used when communicating on the returned socket. The proper value can be retrieved by name by using <A HREF="function.getprotobyname.html" ><B CLASS="function" >getprotobyname()</B ></A >. If the desired protocol is TCP, or UDP the corresponding constants <TT CLASS="constant" ><B >SOL_TCP</B ></TT >, and <TT CLASS="constant" ><B >SOL_UDP</B ></TT > can also be used. </P ><DIV CLASS="table" ><A NAME="AEN212999" ></A ><P ><B >表 3. Common protocols</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD >icmp</TD ><TD > The 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. </TD ></TR ><TR ><TD >udp</TD ><TD > The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead. </TD ></TR ><TR ><TD >tcp</TD ><TD > The 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. </TD ></TR ></TBODY ></TABLE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN213017" ></A ><P ><B >例 1. <B CLASS="function" >socket_create_pair()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$sockets </font><font color="#007700">= array();<br /></font><font color="#FF8000">/* Setup socket pair */<br /></font><font color="#007700">if (</font><font color="#0000BB">socket_create_pair</font><font color="#007700">(</font><font color="#0000BB">AF_UNIX</font><font color="#007700">, </font><font color="#0000BB">SOCK_STREAM</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">$sockets</font><font color="#007700">) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"socket_create_pair failed. Reason: "</font><font color="#007700">.</font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">());<br />}<br /></font><font color="#FF8000">/* Send and Recieve Data */<br /></font><font color="#007700">if (</font><font color="#0000BB">socket_write</font><font color="#007700">(</font><font color="#0000BB">$sockets</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#DD0000">"ABCdef123\n"</font><font color="#007700">, </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#DD0000">"ABCdef123\n"</font><font color="#007700">)) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"socket_write() failed. Reason: "</font><font color="#007700">.</font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">(</font><font color="#0000BB">$sockets</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]));<br />}<br />if ((</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">socket_read</font><font color="#007700">(</font><font color="#0000BB">$sockets</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">], </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#DD0000">"ABCdef123\n"</font><font color="#007700">), </font><font color="#0000BB">PHP_BINARY_READ</font><font color="#007700">) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"socket_read() failed. Reason: "</font><font color="#007700">.</font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">(</font><font color="#0000BB">$sockets</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]));<br />}<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Close sockets */<br /></font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$sockets</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]);<br /></font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$sockets</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN213022" ></A ><P ><B >例 2. <B CLASS="function" >socket_create_pair()</B > IPC example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ary </font><font color="#007700">= array();<br /></font><font color="#0000BB">$strone </font><font color="#007700">= </font><font color="#DD0000">'Message From Parent.'</font><font color="#007700">;<br /></font><font color="#0000BB">$strtwo </font><font color="#007700">= </font><font color="#DD0000">'Message From Child.'</font><font color="#007700">;<br />if (</font><font color="#0000BB">socket_create_pair</font><font color="#007700">(</font><font color="#0000BB">AF_UNIX</font><font color="#007700">, </font><font color="#0000BB">SOCK_STREAM</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">$ary</font><font color="#007700">) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"socket_create_pair() failed. Reason: "</font><font color="#007700">.</font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">());<br />}<br /></font><font color="#0000BB">$pid </font><font color="#007700">= </font><font color="#0000BB">pcntl_fork</font><font color="#007700">();<br />if (</font><font color="#0000BB">$pid </font><font color="#007700">== -</font><font color="#0000BB">1</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'Could not fork Process.'</font><font color="#007700">;<br />} elseif (</font><font color="#0000BB">$pid</font><font color="#007700">) {<br /> </font><font color="#FF8000">/*parent*/<br /> </font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]);<br /> if (</font><font color="#0000BB">socket_write</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">], </font><font color="#0000BB">$strone</font><font color="#007700">, </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$strone</font><font color="#007700">)) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"socket_write() failed. Reason: "</font><font color="#007700">.</font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]));<br /> }<br /> if (</font><font color="#0000BB">socket_read</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">], </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$strtwo</font><font color="#007700">), </font><font color="#0000BB">PHP_BINARY_READ</font><font color="#007700">) == </font><font color="#0000BB">$strtwo</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Recieved $strtwo</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]);<br />} else {<br /> </font><font color="#FF8000">/*child*/<br /> </font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]);<br /> if (</font><font color="#0000BB">socket_write</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">$strtwo</font><font color="#007700">, </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$strtwo</font><font color="#007700">)) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"socket_write() failed. Reason: "</font><font color="#007700">.</font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">socket_last_error</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]));<br /> }<br /> if (</font><font color="#0000BB">socket_read</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$strone</font><font color="#007700">), </font><font color="#0000BB">PHP_BINARY_READ</font><font color="#007700">) == </font><font color="#0000BB">$strone</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Recieved $strone</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$ary</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]);<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.socket-create-listen.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.socket-create.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >socket_create_listen</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.sockets.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >socket_create</TD ></TR ></TABLE ></DIV ></BODY ></HTML >