<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// The following content is unrelated to the main article</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"This is a sample PHP code snippet used to demonstrate a preface unrelated to the article.\n"</span></span><span>;
<hr>
<p>/**</p>
<ul>
<li>
<p>How to Use stream_set_blocking to Control Socket Reading Behavior in PHP and Improve Network Application Performance?</p>
</li>
<li></li>
<li>
<p>In PHP network programming, the way sockets (streams) handle reading directly affects application performance. By default,</p>
</li>
<li>
<p>PHP socket operations are blocking, meaning when calling read functions (such as fread or stream_get_contents),</p>
</li>
<li>
<p>if no data has arrived, the program will wait until data becomes available. This behavior can easily cause performance bottlenecks</p>
</li>
<li>
<p>in high-concurrency or long-connection scenarios.</p>
</li>
<li></li>
<li>
<p>To address this issue, PHP provides the stream_set_blocking function, which allows flexible control of socket blocking modes.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Basic Usage of stream_set_blocking</p>
</li>
</ol>
</li>
<li></li>
<li>
<p>Function prototype:</p>
</li>
<li>
<p>bool stream_set_blocking ( resource $stream , int $mode )</p>
</li>
<li></li>
<li>
<p>Parameter explanation:</p>
</li>
<li>
<ul>
<li>
<p>$stream: The socket resource (stream resource).</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$mode: Blocking mode, 1 means blocking, 0 means non-blocking.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Example:<br>
*/<br>
</span>$socket = stream_socket_client("tcp://127.0.0.1:8080", $errno, </span>$errstr, </span>30);<br>
</span>if (!$socket) {<br>
</span>echo </span>"Connection failed: $errstr ($errno)\n";<br>
} else {<br>
</span>// Set to non-blocking mode<br>
</span>stream_set_blocking(</span>$socket, </span>0);</p>
<p></span>// Attempt to read data<br>
</span>$data = </span>fread(</span>$socket, </span>8192);<br>
</span>if (</span>$data === </span>false) {<br>
</span>echo </span>"No readable data available, program continues running.\n";<br>
} </span>else {<br>
</span>echo </span>"Data received: $data\n";<br>
}<br>
}</p>
</li>
</ul>
<hr>
<p>/**</p>
<ul>
<li>
<p>2. Differences Between Blocking and Non-blocking Modes</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Blocking mode</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>fread or fgets will wait until data arrives before returning.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Suitable for single-threaded, low-concurrency scenarios.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Simple, but may cause long waits and block the entire script.</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Non-blocking mode</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>fread and similar functions return immediately; if no data, they return empty or false.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Suitable for asynchronous processing and high-concurrency scenarios.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Can be combined with stream_select to monitor multiple sockets’ readability, improving performance.</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="3">
<li>
<p>Improving Performance with stream_select</p>
</li>
</ol>
</li>
<li></li>
<li>
<p>stream_select can monitor multiple streams for readability, writability, or exceptions:</p>
</li>
<li></li>
<li>
<p>Example:<br>
*/<br>
$read = [</span>$socket];<br>
</span>$write = </span>null;<br>
</span>$except = </span>null;<br>
</span>$timeout = </span>2; </span>// Timeout of 2 seconds</p>
</li>
</ul>
<h2></span>$num_changed_streams = </span>stream_select(</span>$read, </span>$write, </span>$except, </span>$timeout);<br>
</span>if (</span>$num_changed_streams === </span>false) {<br>
</span>echo </span>"Error monitoring socket\n";<br>
} </span>elseif (</span>$num_changed_streams === </span>0) {<br>
</span>echo </span>"Timeout, no readable data\n";<br>
} </span>else {<br>
</span>foreach (</span>$read </span>as </span>$r) {<br>
</span>$data = </span>fread(</span>$r, </span>8192);<br>
</span>echo </span>"Data received: $data\n";<br>
}<br>
}</h2>
<p>/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>4. Performance Optimization Tips in Real Applications</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>In high-concurrency network applications, prefer non-blocking mode for reading sockets to prevent a single slow connection from blocking the entire process.</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>Use stream_select or similar event-loop mechanisms (such as ReactPHP) to poll multiple sockets.</p>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>
<p>For long connections, set reasonable read timeouts to avoid infinite blocking.</p>
</li>
</ol>
</li>
<li>
<ol start="4">
<li>
<p>Avoid handling large amounts of data in blocking mode; if necessary, read in chunks or use buffering.</p>
</li>
</ol>
</li>
<li></li>
<li data-is-last-node="">
<p data-is-last-node="">By making proper use of stream_set_blocking and event-loop mechanisms, you can significantly improve the responsiveness and throughput of PHP network applications.<br>
*/<br>
?><br>
</span>