Current Location: Home> Latest Articles> Detailed Guide and Usage Steps of the stream_socket_recvfrom Function

Detailed Guide and Usage Steps of the stream_socket_recvfrom Function

gitbox 2025-08-04
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This part of the code is unrelated to the article content and serves only as an example</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Welcome to this PHP tutorial article."</span></span><span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/*</p>
<ul>
<li>
<p>Title: Detailed Guide and Usage Steps of the stream_socket_recvfrom Function</p>
</li>
<li></li>
<li>
<p>In PHP, stream_socket_recvfrom is a function used to receive data from a network socket,</p>
</li>
<li>
<p>commonly utilized in UDP or connectionless socket communication.</p>
</li>
<li>
<p>It reads data from the specified socket resource and can also return the sender’s address information,</p>
</li>
<li>
<p>which is useful for network data interaction.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Function Definition</p>
</li>
</ol>
</li>
<li>
<p>int stream_socket_recvfrom(resource $socket, int $length, int $flags = 0, string &$address = null)</p>
</li>
<li></li>
<li>
<p>Parameters:</p>
</li>
<li>
<ul>
<li>
<p>$socket: The socket resource to read data from.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$length: The maximum number of bytes to read.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$flags: Optional, flags controlling the read behavior, default is 0.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>&$address: Optional, passed by reference, used to receive the sender’s address information (IP and port).</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Return value:</p>
</li>
<li>
<p>Returns the actual number of bytes read, or false on failure.</p>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Usage Steps and Example</p>
</li>
</ol>
</li>
<li></li>
<li>
<ol>
<li>
<p>Create a UDP socket (can also be used for TCP, but recvfrom is typically for connectionless protocols)</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>Bind to a local address and port to listen for data</p>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>
<p>Call stream_socket_recvfrom to receive data</p>
</li>
</ol>
</li>
<li>
<ol start="4">
<li>
<p>Handle the received data and sender’s address</p>
</li>
</ol>
</li>
<li></li>
<li>
<p>Example code:<br>
*/</p>
</li>
</ul>
<p>$socket = stream_socket_server("udp://0.0.0.0:9999", $errno, $errstr, STREAM_SERVER_BIND);<br>
if (!$socket) {<br>
die("Failed to create UDP socket: $errstr" ($errno)");<br>
}</p>
<p>echo "UDP server started, listening on port 9999...\n";</p>
<p>while (true) {<br>
$buf = stream_socket_recvfrom($socket, 1024, 0, $peer);<br>
if ($buf === false) {<br>
echo "Failed to receive data\n";<br>
break;<br>
}<br>
echo "Received data from $peer: $buf\n";</p>
</span><span><span class="hljs-variable">$response</span></span><span> = </span><span><span class="hljs-string">"Data received: "</span></span><span> . </span><span><span class="hljs-variable">$buf</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">stream_socket_sendto</span></span><span>(</span><span><span class="hljs-variable">$socket</span></span><span>, </span><span><span class="hljs-variable">$response</span></span><span>, </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-variable">$peer</span></span><span>);

}

/*

  • 3. Notes on Usage

    • stream_socket_recvfrom is suited for connectionless protocols like UDP; for TCP connections, stream_socket_recv is more common.

    • Handle the case when it returns false to avoid unexpected program crashes.

    • The $address parameter is passed by reference to conveniently get sender information.

    • Passing an excessively large $length may affect performance; it's advisable to set it reasonably based on actual data size.

  • Summary:

  • stream_socket_recvfrom is a common PHP function for handling UDP packet reception,

  • flexible and capable of retrieving sender information,

  • suitable for implementing simple UDP servers or network communication features.
    */
    ?>