<!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>Makes an HTTP server accept connections on the specified socket stream or resource</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.eventhttp.html">EventHttp</a></div> <div class="next" style="text-align: right; float: right;"><a href="eventhttp.addserveralias.html">EventHttp::addServerAlias</a></div> <div class="up"><a href="class.eventhttp.html">EventHttp</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="eventhttp.accept" class="refentry"> <div class="refnamediv"> <h1 class="refname">EventHttp::accept</h1> <p class="verinfo">(PECL event >= 1.2.6-beta)</p><p class="refpurpose"><span class="refname">EventHttp::accept</span> — <span class="dc-title">Makes an HTTP server accept connections on the specified socket stream or resource</span></p> </div> <div class="refsect1 description" id="refsect1-eventhttp.accept-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>EventHttp::accept</strong></span> ( <span class="methodparam"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$socket</code> </span> )</div> <p class="para rdfs-comment"> Makes an HTTP server accept connections on the specified socket stream or resource. The socket should be ready to accept connections. </p> <p class="para"> Can be called multiple times to accept connections on different sockets. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> To bind a socket, <em>listen</em> , and <em> accept</em> connections on the socket in s single call use <span class="methodname"><a href="eventhttp.bind.html" class="methodname">EventHttp::bind()</a></span> . <span class="methodname"><strong>EventHttp::accept()</strong></span> is needed only if one already has a socket ready to accept connections. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-eventhttp.accept-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"> <em><code class="parameter">socket</code></em> </span> <dd> <p class="para"> Socket resource, stream or numeric file descriptor representing a socket ready to accept connections. </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-eventhttp.accept-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success. Otherwise <strong><code>FALSE</code></strong>. </p> </div> <div class="refsect1 examples" id="refsect1-eventhttp.accept-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-4261"> <p><strong>Example #1 <span class="function"><strong>EventHttp::accept()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$base </span><span style="color: #007700">= new </span><span style="color: #0000BB">EventBase</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$http </span><span style="color: #007700">= new </span><span style="color: #0000BB">EventHttp</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$addresses </span><span style="color: #007700">= array (<br /> </span><span style="color: #0000BB">8091 </span><span style="color: #007700">=> </span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">8092 </span><span style="color: #007700">=> </span><span style="color: #DD0000">"127.0.0.2"</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$socket </span><span style="color: #007700">= array();<br /><br />foreach (</span><span style="color: #0000BB">$addresses </span><span style="color: #007700">as </span><span style="color: #0000BB">$port </span><span style="color: #007700">=> </span><span style="color: #0000BB">$ip</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$ip</span><span style="color: #007700">, </span><span style="color: #DD0000">" "</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$socket</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">] = </span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">, </span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">, </span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br /> if (!</span><span style="color: #0000BB">socket_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">], </span><span style="color: #0000BB">$ip</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">)) {<br /> exit(</span><span style="color: #DD0000">"socket_bind failed\n"</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">socket_listen</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">], </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">socket_set_nonblock</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]);<br /><br /> if (!</span><span style="color: #0000BB">$http</span><span style="color: #007700">-></span><span style="color: #0000BB">accept</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">])) {<br /> echo </span><span style="color: #DD0000">"Accept failed\n"</span><span style="color: #007700">;<br /> exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /> }<br /><br /> ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-></span><span style="color: #0000BB">setCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"/some-page"</span><span style="color: #007700">, function() {<br /> echo </span><span style="color: #DD0000">"(some-page)\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"URI: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">-></span><span style="color: #0000BB">getUri</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$req</span><span style="color: #007700">-></span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">, </span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-></span><span style="color: #0000BB">setDefaultCallback</span><span style="color: #007700">(function(</span><span style="color: #0000BB">$req</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"URI: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">-></span><span style="color: #0000BB">getUri</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$req</span><span style="color: #007700">-></span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">, </span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">$signal </span><span style="color: #007700">= </span><span style="color: #0000BB">Event</span><span style="color: #007700">::</span><span style="color: #0000BB">signal</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">, </span><span style="color: #0000BB">SIGINT</span><span style="color: #007700">, function () use (</span><span style="color: #0000BB">$base</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Caught SIGINT. Stopping...\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$base</span><span style="color: #007700">-></span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /></span><span style="color: #0000BB">$signal</span><span style="color: #007700">-></span><span style="color: #0000BB">add</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$base</span><span style="color: #007700">-></span><span style="color: #0000BB">dispatch</span><span style="color: #007700">();<br />echo </span><span style="color: #DD0000">"END\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// We didn't close sockets, since Libevent already sets<br />// CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file <br />// descriptor associated with the sockets.<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> Client: $ nc 127.0.0.1 8091 GET /about HTTP/1.0 Connection: close HTTP/1.0 200 OK Content-Type: text/html; charset=ISO-8859-1 Connection: close Server: 127.0.0.1 8091 127.0.0.2 8092 URI: /about OK </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-eventhttp.accept-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="eventhttp.bind.html" class="methodname" rel="rdfs-seeAlso">EventHttp::bind()</a> - Binds an HTTP server on the specified address and port</span> </li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.eventhttp.html">EventHttp</a></div> <div class="next" style="text-align: right; float: right;"><a href="eventhttp.addserveralias.html">EventHttp::addServerAlias</a></div> <div class="up"><a href="class.eventhttp.html">EventHttp</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>