<!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 indistinguishable sockets and stores them in an array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-create-listen.html">socket_create_listen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-create.html">socket_create</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.socket-create-pair" class="refentry"> <div class="refnamediv"> <h1 class="refname">socket_create_pair</h1> <p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_create_pair</span> — <span class="dc-title">Creates a pair of indistinguishable sockets and stores them in an array</span></p> </div> <div class="refsect1 description" id="refsect1-function.socket-create-pair-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>socket_create_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> , <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$fd</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>socket_create_pair()</strong></span> creates two connected and indistinguishable sockets, and stores them in <em><code class="parameter">fd</code></em>. This function is commonly used in IPC (InterProcess Communication). </p> </div> <div class="refsect1 parameters" id="refsect1-function.socket-create-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 <em><code class="parameter">domain</code></em> parameter specifies the protocol family to be used by the socket. See <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> for the full list. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">type</code></em></span> <dd> <p class="para"> The <em><code class="parameter">type</code></em> parameter selects the type of communication to be used by the socket. See <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> for the full list. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">protocol</code></em></span> <dd> <p class="para"> The <em><code class="parameter">protocol</code></em> parameter sets the specific protocol within the specified <em><code class="parameter">domain</code></em> to be used when communicating on the returned socket. The proper value can be retrieved by name by using <span class="function"><a href="function.getprotobyname.html" class="function">getprotobyname()</a></span>. If the desired protocol is TCP, or UDP the corresponding constants <strong><code>SOL_TCP</code></strong>, and <strong><code>SOL_UDP</code></strong> can also be used. </p> <p class="para"> See <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> for the full list of supported protocols. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">fd</code></em></span> <dd> <p class="para"> Reference to an array in which the two socket resources will be inserted. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.socket-create-pair-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.socket-create-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 re-enabled on Windows platforms. </td> </tr> <tr> <td>4.3.0</td> <td> This function was due to a bug made unavailable on Windows platforms. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.socket-create-pair-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4590"> <p><strong>Example #1 <span class="function"><strong>socket_create_pair()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$sockets </span><span style="color: #007700">= array();<br /><br /></span><span style="color: #FF8000">/* On Windows we need to use AF_INET */<br /></span><span style="color: #0000BB">$domain </span><span style="color: #007700">= (</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_OS</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">)) == </span><span style="color: #DD0000">'WIN' </span><span style="color: #007700">? </span><span style="color: #0000BB">AF_INET </span><span style="color: #007700">: </span><span style="color: #0000BB">AF_UNIX</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Setup socket pair */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">socket_create_pair</span><span style="color: #007700">(</span><span style="color: #0000BB">$domain</span><span style="color: #007700">, </span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$sockets</span><span style="color: #007700">) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_create_pair failed. Reason: "</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">());<br />}<br /></span><span style="color: #FF8000">/* Send and Recieve Data */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">socket_write</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">"ABCdef123\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">"ABCdef123\n"</span><span style="color: #007700">)) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_write() failed. Reason: "</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</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 />if ((</span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">socket_read</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: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">"ABCdef123\n"</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_BINARY_READ</span><span style="color: #007700">)) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_read() failed. Reason: "</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</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">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Close sockets */<br /></span><span style="color: #0000BB">socket_close</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 /></span><span style="color: #0000BB">socket_close</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 /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4591"> <p><strong>Example #2 <span class="function"><strong>socket_create_pair()</strong></span> IPC example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$ary </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$strone </span><span style="color: #007700">= </span><span style="color: #DD0000">'Message From Parent.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$strtwo </span><span style="color: #007700">= </span><span style="color: #DD0000">'Message From Child.'</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">socket_create_pair</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_UNIX</span><span style="color: #007700">, </span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$ary</span><span style="color: #007700">) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_create_pair() failed. Reason: "</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">());<br />}<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 />if (</span><span style="color: #0000BB">$pid </span><span style="color: #007700">== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Could not fork Process.'</span><span style="color: #007700">;<br />} elseif (</span><span style="color: #0000BB">$pid</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/*parent*/<br /> </span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /> if (</span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">], </span><span style="color: #0000BB">$strone</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$strone</span><span style="color: #007700">)) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_write() failed. Reason: "</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]));<br /> }<br /> if (</span><span style="color: #0000BB">socket_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">], </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$strtwo</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_BINARY_READ</span><span style="color: #007700">) == </span><span style="color: #0000BB">$strtwo</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Recieved </span><span style="color: #0000BB">$strtwo</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br />} else {<br /> </span><span style="color: #FF8000">/*child*/<br /> </span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /> if (</span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], </span><span style="color: #0000BB">$strtwo</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$strtwo</span><span style="color: #007700">)) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_write() failed. Reason: "</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]));<br /> }<br /> if (</span><span style="color: #0000BB">socket_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$strone</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_BINARY_READ</span><span style="color: #007700">) == </span><span style="color: #0000BB">$strone</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Recieved </span><span style="color: #0000BB">$strone</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ary</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.socket-create-pair-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.socket-create.html" class="function" rel="rdfs-seeAlso">socket_create()</a> - Create a socket (endpoint for communication)</span></li> <li class="member"> <span class="function"><a href="function.socket-create-listen.html" class="function" rel="rdfs-seeAlso">socket_create_listen()</a> - Opens a socket on port to accept connections</span></li> <li class="member"> <span class="function"><a href="function.socket-bind.html" class="function" rel="rdfs-seeAlso">socket_bind()</a> - Binds a name to a socket</span></li> <li class="member"> <span class="function"><a href="function.socket-listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a> - Listens for a connection on a socket</span></li> <li class="member"> <span class="function"><a href="function.socket-last-error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li> <li class="member"> <span class="function"><a href="function.socket-strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-create-listen.html">socket_create_listen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-create.html">socket_create</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>