Current Location: Home> Latest Articles> How to combine socket_create, socket_bind and socket_set_block

How to combine socket_create, socket_bind and socket_set_block

gitbox 2025-05-28

In PHP's Socket programming, the socket_set_block() function is used to set the socket to blocking mode. In blocking mode, I/O operations such as socket_accept() or socket_read( ) will wait until data is available or the connection is completed, which is especially important in server programs that need to synchronize client requests.

This article will combine socket_create() and socket_bind() to demonstrate how to create a Socket server based on blocking mode.

1. Introduction to socket_set_block

socket_set_block(resource $socket): bool

This function sets the specified socket to blocking mode, and the return value is Boolean, indicating whether the setting is successful.

Blocking mode is the default behavior, but if you have ever called socket_set_nonblock() , you need to use socket_set_block() to restore.

2. Complete example of implementing blocking mode connections

Below is a complete PHP server example, using functions such as socket_create() , socket_bind() , socket_listen() , and socket_accept() , and explicitly set blocking mode.

<code> <?php // Create TCP socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n"); }

// Set to blocking mode
if (!socket_set_block($socket)) {
die("Cannot set to blocking mode: " . socket_strerror(socket_last_error($socket)) . "\n");
}

// Bind IP and port
$host = '0.0.0.0';
$port = 12345;
if (!socket_bind($socket, $host, $port)) {
die("socket_bind() failed: " . socket_strerror(socket_last_error($socket)) . "\n");
}

// Listen to the connection
if (!socket_listen($socket, 5)) {
die("socket_listen() failed: " . socket_strerror(socket_last_error($socket)) . "\n");
}

echo "The server starts, listen in {$host}:{$port}...\n";

while (true) {
echo "Waiting for client connection...\n";
$client = socket_accept($socket);
if ($client === false) {
echo "socket_accept() failed: " . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}

 echo "Client is connected,Reading data...\n";

// Read client messages(Block until data is received)
$input = socket_read($client, 1024);
echo "Received a message: " . trim($input) . "\n";

$response = "Welcome to connect to the server!You sent:" . trim($input);
socket_write($client, $response, strlen($response));

socket_close($client);

}

// Close the main socket
socket_close($socket);
</code>

3. Client testing method

You can use the telnet tool to connect to the server:

 telnet 127.0.0.1 12345

Enter any text and the server will respond to what you sent.

4. Practical application scenarios

Blocking mode is suitable for the following scenarios:

  • Simple serial server model

  • Only one client connection is processed at a time

  • Services that require little real-time

However, for high concurrent applications, it is usually recommended to use non-blocking mode in conjunction with multi-process or asynchronous methods.

5. Tips

In debugging and deployment, it is recommended to output socket error information in combination with error_log or log files to quickly locate problems. For example, errors can be written to a file:

 error_log("socket_error: " . socket_strerror(socket_last_error()), 3, "/var/log/php_socket_error.log");

6. Summary

Through socket_set_block() , we can flexibly control the I/O behavior of Socket. In some cases, explicitly setting blocking mode can avoid exception problems due to default behavior modifications. When combined with socket_create() and socket_bind() , ensure that the logical order is correct, that is, first create, then bind, and then set the listening and blocking mode, thereby building a stable Socket server program.

In the above example, if you deploy it to the server, you can send a connection test to http://gitbox.net:12345 through a browser or API request (actually according to your IP and port configuration).

Hope this article will be helpful for you to understand the usage of socket_set_block() .