Current Location: Home> Latest Articles> Create a blocking server in PHP: From socket_create to socket_set_block

Create a blocking server in PHP: From socket_create to socket_set_block

gitbox 2025-05-26

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.

1. Preparation: Ensure Socket extension is enabled

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.

2. Create a TCP server using socket

The following are the basic steps to create a socket and step by step introduce the role of each function.

1. Create a Socket

 $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 ).

2. Set Socket options (optional)

Some socket options can be set, such as port reuse:

 socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

3. Bind Socket

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

4. Listen to the port

 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.

3. Set to blocking mode

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.

4. Accept connection and read data

 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);
}

5. Shut down the server

Don't forget to close the main socket when you need to exit:

 socket_close($socket);

6. Access test

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.

Summarize

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.