<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Creates a pair of connected, indistinguishable socket streams</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-get-name.html">stream_socket_get_name</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-socket-recvfrom.html">stream_socket_recvfrom</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.stream-socket-pair" class="refentry"> <div class="refnamediv"> <h1 class="refname">stream_socket_pair</h1> <p class="verinfo">(PHP 5 >= 5.1.0)</p><p class="refpurpose"><span class="refname">stream_socket_pair</span> — <span class="dc-title"> Creates a pair of connected, indistinguishable socket streams </span></p> </div> <div class="refsect1 description" id="refsect1-function.stream-socket-pair-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>stream_socket_pair</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$domain</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$protocol</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>stream_socket_pair()</strong></span> creates a pair of connected, indistinguishable socket streams. This function is commonly used in IPC (Inter-Process Communication). </p> </div> <div class="refsect1 parameters" id="refsect1-function.stream-socket-pair-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">domain</code></em></span> <dd> <p class="para"> The protocol family to be used: <strong><code>STREAM_PF_INET</code></strong>, <strong><code>STREAM_PF_INET6</code></strong> or <strong><code>STREAM_PF_UNIX</code></strong> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">type</code></em></span> <dd> <p class="para"> The type of communication to be used: <strong><code>STREAM_SOCK_DGRAM</code></strong>, <strong><code>STREAM_SOCK_RAW</code></strong>, <strong><code>STREAM_SOCK_RDM</code></strong>, <strong><code>STREAM_SOCK_SEQPACKET</code></strong> or <strong><code>STREAM_SOCK_STREAM</code></strong> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">protocol</code></em></span> <dd> <p class="para"> The protocol to be used: <strong><code>STREAM_IPPROTO_ICMP</code></strong>, <strong><code>STREAM_IPPROTO_IP</code></strong>, <strong><code>STREAM_IPPROTO_RAW</code></strong>, <strong><code>STREAM_IPPROTO_TCP</code></strong> or <strong><code>STREAM_IPPROTO_UDP</code></strong> </p> </dd> </dt> </dl> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Please consult the <a href="stream.constants.html" class="link">Streams constant list</a> for further details on each constant. </span> </p></blockquote> </div> <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-pair-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> with the two socket resources on success, or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.stream-socket-pair-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> This function is now available on Windows platforms. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.stream-socket-pair-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4036"> <p><strong>Example #1 A <span class="function"><strong>stream_socket_pair()</strong></span> example</strong></p> <div class="example-contents"><p> This example shows the basic usage of <span class="function"><strong>stream_socket_pair()</strong></span> in Inter-Process Comunication. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$sockets </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_pair</span><span style="color: #007700">(</span><span style="color: #0000BB">STREAM_PF_UNIX</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_SOCK_STREAM</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_IPPROTO_IP</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pid </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br /><br />if (</span><span style="color: #0000BB">$pid </span><span style="color: #007700">== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">'could not fork'</span><span style="color: #007700">);<br /><br />} else if (</span><span style="color: #0000BB">$pid</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* parent */<br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">], </span><span style="color: #DD0000">"child PID: </span><span style="color: #0000BB">$pid</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br /> echo </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />} else {<br /> </span><span style="color: #FF8000">/* child */<br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], </span><span style="color: #DD0000">"message from child\n"</span><span style="color: #007700">);<br /> echo </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> child PID: 1378 message from child </pre></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-get-name.html">stream_socket_get_name</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-socket-recvfrom.html">stream_socket_recvfrom</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>