<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.sockets.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.socket-recvfrom.php',
    1 => 'socket_recvfrom',
    2 => 'Receives data from a socket whether or not it is connection-oriented',
  ),
  'up' => 
  array (
    0 => 'ref.sockets.php',
    1 => 'Socket Functions',
  ),
  'prev' => 
  array (
    0 => 'function.socket-recv.php',
    1 => 'socket_recv',
  ),
  'next' => 
  array (
    0 => 'function.socket-recvmsg.php',
    1 => 'socket_recvmsg',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/sockets/functions/socket-recvfrom.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.socket-recvfrom" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_recvfrom</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">socket_recvfrom</span> &mdash; <span class="dc-title">Receives data from a socket whether or not it is connection-oriented</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-recvfrom-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_recvfrom</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.socket.php" class="type Socket">Socket</a></span> <code class="parameter">$socket</code></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">&$data</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">$length</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">$flags</code></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">&$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">&$port</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.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   The <span class="function"><strong>socket_recvfrom()</strong></span> function receives
   <code class="parameter">length</code> bytes of data in <code class="parameter">data</code> from
   <code class="parameter">address</code> on port <code class="parameter">port</code> (if the
   socket is not of type <strong><code><a href="sockets.constants.php#constant.af-unix">AF_UNIX</a></code></strong>) using
   <code class="parameter">socket</code>. <span class="function"><strong>socket_recvfrom()</strong></span> can be
   used to gather data from both connected and unconnected sockets.
   Additionally, one or more flags can be specified to modify the behaviour of
   the function.
  </p>
  <p class="para">
   The <code class="parameter">address</code> and <code class="parameter">port</code> must be
   passed by reference. If the socket is not connection-oriented,
   <code class="parameter">address</code> will be set to the internet protocol address of
   the remote host or the path to the UNIX socket. If the socket is
   connection-oriented, <code class="parameter">address</code> is <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. Additionally,
   the <code class="parameter">port</code> will contain the port of the remote host in
   the case of an unconnected <strong><code><a href="sockets.constants.php#constant.af-inet">AF_INET</a></code></strong> or
   <strong><code><a href="sockets.constants.php#constant.af-inet6">AF_INET6</a></code></strong> socket. 
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is
binary-safe.</span></p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-recvfrom-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">socket</code></dt>
     <dd>
      <p class="para">
       The <code class="parameter">socket</code> must be a <span class="classname"><a href="class.socket.php" class="classname">Socket</a></span> instance previously
       created by socket_create().
      </p>
     </dd>
    

    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       The data received will be fetched to the variable specified with
       <code class="parameter">data</code>.
      </p>
     </dd>
    

    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       Up to <code class="parameter">length</code> bytes will be fetched from remote host.
      </p>
     </dd>
    

    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       The value of <code class="parameter">flags</code> can be any combination of 
       the following flags, joined with the binary OR (<code class="literal">|</code>)
       operator.
      </p>
      
      <table class="doctable table">
       <caption><strong>Possible values for <code class="parameter">flags</code></strong></caption>
       
        <thead>
         <tr>
          <th>Flag</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.msg-oob">MSG_OOB</a></code></strong></td>
          <td>
           Process out-of-band data.
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.msg-peek">MSG_PEEK</a></code></strong></td>
          <td>
           Receive data from the beginning of the receive queue without
           removing it from the queue.
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.msg-waitall">MSG_WAITALL</a></code></strong></td>
          <td>
           Block until at least <code class="parameter">length</code> are received.
           However, if a signal is caught or the remote host disconnects, the
           function may return less data.
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.msg-dontwait">MSG_DONTWAIT</a></code></strong></td>
          <td>
           With this flag set, the function returns even if it would normally
           have blocked.
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>
    

    
     <dt><code class="parameter">address</code></dt>
     <dd>
      <p class="para">
       If the socket is of the type <strong><code><a href="sockets.constants.php#constant.af-unix">AF_UNIX</a></code></strong> type,
       <code class="parameter">address</code> is the path to the file. Else, for
       unconnected sockets, <code class="parameter">address</code> is the IP address of,
       the remote host, or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> if the socket is connection-oriented.
      </p>
     </dd>
    

    
     <dt><code class="parameter">port</code></dt>
     <dd>
      <p class="para">
       This argument only applies to <strong><code><a href="sockets.constants.php#constant.af-inet">AF_INET</a></code></strong> and
       <strong><code><a href="sockets.constants.php#constant.af-inet6">AF_INET6</a></code></strong> sockets, and specifies the remote port
       from which the data is received. If the socket is connection-oriented,
       <code class="parameter">port</code> will be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
      </p>
     </dd>
    
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.socket-recvfrom-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <span class="function"><strong>socket_recvfrom()</strong></span> returns the number of bytes received,
   or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if there was an error. The actual error code can be retrieved by 
   calling <span class="function"><a href="function.socket-last-error.php" class="function">socket_last_error()</a></span>. This error code may be
   passed to <span class="function"><a href="function.socket-strerror.php" class="function">socket_strerror()</a></span> to get a textual explanation
   of the error.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.socket-recvfrom-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
  <td>8.0.0</td>
  <td>
   <code class="parameter">socket</code> is a <span class="classname"><a href="class.socket.php" class="classname">Socket</a></span> instance now;
   previously, it was a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>.
  </td>
 </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-recvfrom-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>socket_recvfrom()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$socket </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_DGRAM</span><span style="color: #007700">, </span><span style="color: #0000BB">SOL_UDP</span><span style="color: #007700">);<br /></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: #DD0000">'127.0.0.1'</span><span style="color: #007700">, </span><span style="color: #0000BB">1223</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$from </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$port </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$buf</span><span style="color: #007700">, </span><span style="color: #0000BB">12</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$from</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Received </span><span style="color: #0000BB">$buf</span><span style="color: #DD0000"> from remote address </span><span style="color: #0000BB">$from</span><span style="color: #DD0000"> and remote port </span><span style="color: #0000BB">$port</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>
     This example will initiate a UDP socket on port 1223 of 127.0.0.1
     and print at most 12 characters received from a remote host.
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-recvfrom-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.socket-recv.php" class="function" rel="rdfs-seeAlso">socket_recv()</a> - Receives data from a connected socket</span></li>
    <li><span class="function"><a href="function.socket-send.php" class="function" rel="rdfs-seeAlso">socket_send()</a> - Sends data to a connected socket</span></li>
    <li><span class="function"><a href="function.socket-sendto.php" class="function" rel="rdfs-seeAlso">socket_sendto()</a> - Sends a message to a socket, whether it is connected or not</span></li>
    <li><span class="function"><a href="function.socket-create.php" class="function" rel="rdfs-seeAlso">socket_create()</a> - Create a socket (endpoint for communication)</span></li>
   </ul>
  </p>
 </div>

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