Current Location: Home> Latest Articles> Use socket_set_block to build blocking Socket chat programs

Use socket_set_block to build blocking Socket chat programs

gitbox 2025-05-29

In PHP, socket programming is one of the basic means to realize network communication. By default, sockets may be non-blocking, meaning that when there is no data, the read operation returns immediately instead of waiting for data to arrive. The blocking Socket will make the program wait while reading data until the data is read, which is suitable for implementing a simple synchronous chat program.

This article will introduce how to use PHP's socket_set_block function to set Socket to blocking mode and implement a simple blocking chat program example based on this.

What is a blocking socket?

A blocking Socket refers to the program pausing and waiting until data arrives or operation is completed when performing a read or write operation. This method is simple to implement and clear logic, making it ideal for beginners and small-scale chat applications.

In contrast, non-blocking Sockets require event polling or asynchronous operations, which are logically complex but are more suitable for high-performance requirements.

Introduction to socket_set_block function

socket_set_block is a function in PHP that sets Socket blocking mode. After calling this function, the Socket will switch to the blocking state.

Function prototype:

 bool socket_set_block(resource $socket)
  • $socket : The socket resource that needs to be set

  • Return value: Return true for success, return false for failure

Blocking Socket Chat Program Example

Below is an example of a simple blocking Socket chat server. The client can send messages after connecting, and the server will simply echo after receiving them.

 <?php
$host = "0.0.0.0";
$port = 12345;

// create Socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock === false) {
    die("socket_create() fail: " . socket_strerror(socket_last_error()));
}

// Bind address and port
if (!socket_bind($sock, $host, $port)) {
    die("socket_bind() fail: " . socket_strerror(socket_last_error($sock)));
}

// Listen to port
if (!socket_listen($sock, 5)) {
    die("socket_listen() fail: " . socket_strerror(socket_last_error($sock)));
}

echo "Server startup,monitor {$host}:{$port}...\n";

// Accept client connections
$client = socket_accept($sock);
if ($client === false) {
    die("socket_accept() fail: " . socket_strerror(socket_last_error($sock)));
}

// Setting up the client Socket In blocking mode
socket_set_block($client);

echo "Client connection is successful,Waiting for the message...\n";

while (true) {
    // Blocking reading of client data
    $data = socket_read($client, 2048, PHP_NORMAL_READ);
    if ($data === false) {
        echo "socket_read() fail: " . socket_strerror(socket_last_error($client)) . "\n";
        break;
    }

    $data = trim($data);
    if ($data === '') {
        continue;
    }
    
    echo "Received a client message: $data\n";

    if ($data === 'quit') {
        echo "Client disconnection\n";
        break;
    }

    // Echo the message to the client
    $response = "The server has been received: $data\n";
    socket_write($client, $response, strlen($response));
}

// closure Socket connect
socket_close($client);
socket_close($sock);
?>

Run and test

  1. Save the above code as server.php and run it on the command line:

 php server.php
  1. Connect using telnet or other TCP client:

 telnet gitbox.net 12345

(In this example, the port is 12345, and the domain name example is changed to gitbox.net )

  1. Enter a message, the server will block and wait for your input, and return to confirm after receiving the message.

  2. Enter quit to disconnect.

Summarize

Using socket_set_block makes it very convenient to set Socket to blocking mode, enabling simple synchronous chat logic. Blocking programming is suitable for beginners to quickly get started with network programming, but in high concurrency scenarios, non-blocking or asynchronous methods are more efficient.

For more information about PHP Socket programming, you can visit:

 https://gitbox.net/manual/socket