In PHP development, performance and response speed are key considerations when handling long connections. PHP default socket operation is blocking, which means that when the program calls a read or write operation, it will wait until the operation is completed, which can easily cause performance bottlenecks when handling long connections. To this end, the socket_set_block function is used reasonably to adjust the socket's blocking mode, which can effectively optimize the performance of PHP long connections.
socket_set_block is a function in PHP that sets socket blocking mode. Its purpose is to keep the socket blocking. In blocking mode, the read or write operation will wait until data is ready or the operation is completed before returning.
bool socket_set_block ( resource $socket )
Parameter $socket : The socket resource to set blocking mode.
Return value: Return true if successful, false if failed.
Corresponding to this is socket_set_nonblock , which is used to set non-blocking mode.
Blocking mode : When performing read and write operations, the program waits until the operation is completed or timed out.
Non-blocking mode : When performing read and write operations, the program returns immediately, and there may be no data.
In a long connection, if blocking mode is used, the program will hang while waiting for data, resulting in the inability to process other requests in time. Although non-blocking mode can improve concurrency, it requires additional polling and state judgment to increase code complexity.
Reasonably switching between blocking and non-blocking modes allows PHP to take into account both performance and responsiveness in long connections. Specific strategies include:
When the connection is just established, it is set to non-blocking mode to prevent the program from hanging due to waiting for data.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "gitbox.net", 80);
socket_set_nonblock($socket);
For example, when preparing to read fixed-length data, use blocking mode to ensure that the data is read intact and avoid half-packet problems.
socket_set_block($socket);
$data = socket_read($socket, 1024);
Use socket_select to detect whether the socket has data to read and reduce invalid waiting.
$read = [$socket];
$write = $except = null;
if (socket_select($read, $write, $except, 5) > 0) {
socket_set_block($socket);
$data = socket_read($socket, 1024);
// Processing data
} else {
// Timeout processing or other logic
}
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create failed: " . socket_strerror(socket_last_error()));
}
$result = socket_connect($socket, "gitbox.net", 80);
if ($result === false) {
die("socket_connect failed: " . socket_strerror(socket_last_error($socket)));
}
// Set non-blocking first,Avoid program stuck
socket_set_nonblock($socket);
// Send a request
$request = "GET / HTTP/1.1\r\nHost: gitbox.net\r\nConnection: keep-alive\r\n\r\n";
socket_write($socket, $request, strlen($request));
// use socket_select Wait for the data to be readable
$read = [$socket];
$write = $except = null;
$timeout_sec = 5;
if (socket_select($read, $write, $except, $timeout_sec) > 0) {
// Switch to blocking mode when data is ready,Ensure complete data is read
socket_set_block($socket);
$response = '';
while ($out = socket_read($socket, 2048)) {
$response .= $out;
if (strlen($out) < 2048) {
break;
}
}
echo $response;
} else {
echo "Waiting for data timeout or no data";
}
socket_close($socket);
?>
socket_set_block allows socket operations to enter blocking mode, suitable for reading data of determined length and ensuring integrity.
Reasonably match non-blocking mode and socket_select to avoid unnecessary waiting of programs in long connections and improve performance.
The key to optimizing long connection performance is to flexibly control blocking and non-blocking, and combine it with event detection mechanism to reduce resource waste.
Through the above methods, PHP long connection processing will be more efficient and stable, suitable for network application scenarios with high performance and response time requirements.