<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.stream.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.stream-socket-server.php',
    1 => 'stream_socket_server',
    2 => 'Create an Internet or Unix domain server socket',
  ),
  'up' => 
  array (
    0 => 'ref.stream.php',
    1 => 'Stream Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.stream-socket-sendto.php',
    1 => 'stream_socket_sendto',
  ),
  'next' => 
  array (
    0 => 'function.stream-socket-shutdown.php',
    1 => 'stream_socket_shutdown',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/stream/functions/stream-socket-server.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.stream-socket-server" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_server</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">stream_socket_server</span> &mdash; <span class="dc-title">Create an Internet or Unix domain server socket</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-socket-server-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_socket_server</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$address</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$error_code</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter reference">&$error_message</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Creates a stream or datagram socket on the specified
   <code class="parameter">address</code>.    
  </p>
  <p class="para">
   This function only creates a socket, to begin accepting connections
   use <span class="function"><a href="function.stream-socket-accept.php" class="function">stream_socket_accept()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-server-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">address</code></dt>
     <dd>
      <p class="para">
       The type of socket created is determined by the transport specified
       using standard URL formatting: <code class="literal">transport://target</code>.
      </p>
      <p class="para">
       For Internet Domain sockets (<strong><code><a href="sockets.constants.php#constant.af-inet">AF_INET</a></code></strong>) such as TCP and UDP, the
       <code class="literal">target</code> portion of the 
       <code class="parameter">remote_socket</code> parameter should consist of a
       hostname or IP address followed by a colon and a port number.  For
       Unix domain sockets, the <code class="literal">target</code> portion should
       point to the socket file on the filesystem.
      </p>
      <p class="para">
       Depending on the environment, Unix domain sockets may not be available.
       A list of available transports can be retrieved using
       <span class="function"><a href="function.stream-get-transports.php" class="function">stream_get_transports()</a></span>. See
       <a href="transports.php" class="xref">Elenco dei Trasporti Socket Supportati</a> for a list of bulitin transports.
      </p>
     </dd>
    
    
     <dt><code class="parameter">error_code</code></dt>
     <dd>
      <p class="para">
       If the optional <code class="parameter">error_code</code> and <code class="parameter">error_message</code>
       arguments are present they will be set to indicate the actual system
       level error that occurred in the system-level <code class="literal">socket()</code>,
       <code class="literal">bind()</code>, and <code class="literal">listen()</code> calls. If
       the value returned in <code class="parameter">error_code</code> is 
       <code class="literal">0</code> and the function returned <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, it is an
       indication that the error occurred before the <code class="literal">bind()</code>
       call. This is most likely due to a problem initializing the socket. 
       Note that the <code class="parameter">error_code</code> and
       <code class="parameter">error_message</code> arguments will always be passed by reference.
      </p>
     </dd>
    
    
     <dt><code class="parameter">error_message</code></dt>
     <dd>
      <p class="para">
       See <code class="parameter">error_code</code> description.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       A bitmask field which may be set to any combination of socket creation
       flags.
      </p>
      <blockquote class="note"><p><strong class="note">Nota</strong>: 
       <p class="para">
        For UDP sockets, you must use <strong><code><a href="stream.constants.php#constant.stream-server-bind">STREAM_SERVER_BIND</a></code></strong> as
        the <code class="parameter">flags</code> parameter.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-server-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns the created stream, or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on error.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-socket-server-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">context</code> is nullable now.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-socket-server-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Using TCP server sockets</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$socket </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://0.0.0.0:8000"</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$socket</span><span style="color: #007700">) {<br />  echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br /&gt;\n"</span><span style="color: #007700">;<br />} else {<br />  while (</span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_accept</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)) {<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'The local time is ' </span><span style="color: #007700">. </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'n/j/Y g:i a'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br />  }<br />  </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   The example below shows how to act as a time server which can respond
   to time queries as shown in an example on <span class="function"><a href="function.stream-socket-client.php" class="function">stream_socket_client()</a></span>.
   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <span class="simpara">
     Most systems require root access to create a server socket on a port
     below 1024.
    </span>
   </p></blockquote>
   <div class="example" id="example-2">
    <p><strong>Example #2 Using UDP server sockets</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$socket </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1:1113"</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_SERVER_BIND</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$socket</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)"</span><span style="color: #007700">);<br />}<br /><br />do {<br />    </span><span style="color: #0000BB">$pkt </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$peer</span><span style="color: #007700">);<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$peer</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">stream_socket_sendto</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"D M j H:i:s Y\r\n"</span><span style="color: #007700">), </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$peer</span><span style="color: #007700">);<br />} while (</span><span style="color: #0000BB">$pkt </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-socket-server-notes">
  <h3 class="title">Note</h3>
  <blockquote class="note"><p><strong class="note">Nota</strong>: <span class="simpara">Nella specifica numerica degli indirizzi IPv6 
(es. fe80::1) occorre racchiudere l&#039;IP tra parentesi quadre. Ad esempio,  
<code class="literal">tcp://[fe80::1]:80</code>.</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-socket-server-seealso">
  <h3 class="title">Vedere anche:</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.stream-socket-client.php" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
   <li><span class="function"><a href="function.stream-set-blocking.php" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - Set blocking/non-blocking mode on a stream</span></li>
   <li><span class="function"><a href="function.stream-set-timeout.php" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
   <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - Prende una riga da un puntatore a file</span></li>
   <li><span class="function"><a href="function.fgetss.php" class="function" rel="rdfs-seeAlso">fgetss()</a> - Prende una riga da un puntatore a file ed elimina i tag HTML</span></li>
   <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Scrittura su file binary-safe</span></li>
   <li><span class="function"><a href="function.fclose.php" class="function" rel="rdfs-seeAlso">fclose()</a> - Chiude un puntatore a file aperto</span></li>
   <li><span class="function"><a href="function.feof.php" class="function" rel="rdfs-seeAlso">feof()</a> - Verifica se &egrave; stata raggiunta la fine del file su un puntatore a file</span></li>
   <li><a href="ref.curl.php" class="link">Curl extension</a></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>