<?php
// This section is unrelated to the article content and can be any PHP code example
echo "Starting program execution...\n";
$time = date("Y-m-d H:i:s");
echo "Current time: <span class="hljs-subst">$time</span>\n";
$randomNumber = rand(1, 100);
echo "Random number generated: <span class="hljs-subst">$randomNumber</span>\n";
<?>
<p><hr></p>
<p><?php<br>
// Main content: Differences in using socket_clear_error in UDP vs TCP sockets</p>
<p>/**</p>
<ul>
<li>
<p>Title: Differences in Using socket_clear_error Between UDP and TCP Sockets</p>
</li>
<li></li>
<li>
<p>PHP provides the socket_clear_error() function to clear the error state on a given socket and return the error code before clearing.</p>
</li>
<li>
<p>It is often used together with socket_last_error() for error handling and debugging.</p>
</li>
<li></li>
<li>
<p>UDP (User Datagram Protocol) and TCP (Transmission Control Protocol) differ significantly in their network communication models,</p>
</li>
<li>
<p>so the usage scenarios and behavior of socket_clear_error() also differ.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Characteristics of TCP sockets</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>Connection-oriented: Requires establishing a connection before communication (three-way handshake).</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Reliable transmission: Lost packets are automatically retransmitted.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Clear error feedback: Any send or receive error is usually reported immediately.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>For TCP sockets, if there is a write failure, connection interruption, or read timeout,</p>
</li>
<li>
<p>socket_last_error() returns a specific error code. Using socket_clear_error() clears these error codes,</p>
</li>
<li>
<p>providing a clean state for subsequent operations. Common uses include:</p>
</li>
<li>
<ul>
<li>
<p>Retrying after a connection exception</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Clearing socket state to avoid residual errors affecting the next communication</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Characteristics of UDP sockets</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>Connectionless: No formal connection is established before sending data.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Unreliable transmission: Packets may be lost or arrive out of order; the protocol does not guarantee delivery.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Limited error feedback: In most cases, sending failures do not return specific errors immediately.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>For UDP sockets, even if sending fails (e.g., destination unreachable), many systems do not return an error immediately.</p>
</li>
<li>
<p>Therefore, socket_last_error() often returns 0 or only in rare cases (such as local network interface errors) returns an error code.</p>
</li>
<li>
<p>Calling socket_clear_error() on UDP sockets has limited effect and is mainly used to:</p>
</li>
<li>
<ul>
<li>
<p>Clear local socket state</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Ensure socket_last_error() returns a clean state for consistent error handling</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="3">
<li>
<p>Usage examples<br>
*/</p>
</li>
</ol>
</li>
</ul>
<p>$tcp_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<br>
$udp_socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);</p>
<p>// TCP connection example<br>
@socket_connect($tcp_socket, "127.0.0.1", 8080);<br>
$tcp_error_before = socket_last_error($tcp_socket);<br>
socket_clear_error($tcp_socket);<br>
$tcp_error_after = socket_last_error($tcp_socket);</p>
<p>// UDP send example<br>
@socket_sendto($udp_socket, "Test data", strlen("Test data"), 0, "127.0.0.1", 8081);<br>
$udp_error_before = socket_last_error($udp_socket);<br>
socket_clear_error($udp_socket);<br>
$udp_error_after = socket_last_error($udp_socket);</p>
<p>echo "TCP error code before clearing: $tcp_error_before, after clearing: $tcp_error_after\n";<br>
echo "UDP error code before clearing: $udp_error_before, after clearing: $udp_error_after\n";</p>
<p>/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>Summary:</p>
</li>
<li>
<ul>
<li>
<p>TCP socket error states are usually directly related to connection reliability; socket_clear_error() effectively clears these errors.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>UDP socket error feedback is limited; socket_clear_error() mainly maintains a consistent error handling mechanism.</p>
</li>
</ul>
</li>
<li data-is-last-node="">
<ul data-is-last-node="">
<li data-is-last-node="">
<p data-is-last-node="">In practical development, using socket_clear_error() for both TCP and UDP is meaningful, but its effect is more pronounced for TCP.<br>
*/<br>
<?><br>