Implementing a Socket-based blocking server in PHP can help us understand the underlying network communication principle. Blocking mode means that the server will wait until data is read or written until data can be processed or the connection is ready. This article will step by step analyze how to use PHP's socket extension, from creating sockets to setting in blocking mode, and build a simple blocking server.
First, make sure socket extension is enabled in the PHP environment. Find and uncomment the following line in php.ini (if any):
extension=sockets
Use the php -m command to check if the extension is enabled:
php -m | grep sockets
If you can see sockets , the extension is enabled.
The following are the basic steps to create a socket and step by step introduce the role of each function.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");
}
Here we are creating a TCP socket ( SOCK_STREAM ) based on IPv4 ( AF_INET ).
Some socket options can be set, such as port reuse:
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
$host = '0.0.0.0';
$port = 9000;
if (!socket_bind($socket, $host, $port)) {
die("socket_bind() failed: " . socket_strerror(socket_last_error($socket)) . "\n");
}
if (!socket_listen($socket, 5)) {
die("socket_listen() failed: " . socket_strerror(socket_last_error($socket)) . "\n");
}
5 indicates the maximum number of pending connection requests.
Use socket_set_block to set socket to blocking mode.
if (!socket_set_block($socket)) {
die("socket_set_block() failed: " . socket_strerror(socket_last_error($socket)) . "\n");
}
Once set to blocking mode, subsequent socket_accept , socket_read and other functions will block until the operation is completed.
echo "The server is started,Listen to port $port...\n";
while (true) {
$clientSocket = socket_accept($socket);
if ($clientSocket === false) {
echo "socket_accept() failed: " . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}
$input = socket_read($clientSocket, 1024);
if ($input === false) {
echo "socket_read() failed: " . socket_strerror(socket_last_error($clientSocket)) . "\n";
} else {
$input = trim($input);
echo "Received a message: $input\n";
$response = "You sent it:$input\n";
socket_write($clientSocket, $response, strlen($response));
}
socket_close($clientSocket);
}
Don't forget to close the main socket when you need to exit:
socket_close($socket);
You can use a command line client such as telnet connection test:
telnet gitbox.net 9000
Or write a simple PHP client for connection testing.
By socket_create to socket_set_block , we build a complete blocking server. This mode is suitable for simple network communication applications, such as teaching, debugging or low-concurrency scenarios. For high concurrency scenarios, it is recommended to use non-blocking mode or use select / poll to achieve more efficient event loops.
Through the practical steps in this article, you can master the entire process of building blocking servers in PHP and lay a solid foundation for further in-depth network programming.