Current Location: Home> Latest Articles> How to use socket_accept() to receive client connections

How to use socket_accept() to receive client connections

M66 2025-05-24

In network programming, the server needs to be able to accept connection requests from the client. PHP provides a series of socket functions to handle underlying network communication, where socket_accept() is an important function to implement server listening and receiving connections. This article will introduce in detail how to use socket_accept() to receive client connections and demonstrate how to use them through examples.

1. Function introduction

socket_accept() is a function in PHP that receives a connection from a socket listening queue. The basic syntax is as follows:

 socket_accept(Socket $socket): Socket|false
  • parameter :

  • Return value :

    • Returns a new socket instance when successful, for communication with the client;

    • Returns false on failure.

2. Basic usage steps

Before using socket_accept() , you usually need to complete the following steps:

  1. Create socket: socket_create()

  2. Bind address and port: socket_bind()

  3. Start listening: socket_listen()

  4. Receive connection: socket_accept()

  5. Communication with clients: socket_read() and socket_write()

  6. Close the connection: socket_close()

3. Sample code

Here is a complete socket server example showing how to use socket_accept() to receive client connections:

 <?php
// Set the address and port
$address = '0.0.0.0';
$port = 12345;

// create socket
$serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($serverSocket === false) {
    die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}

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

// Start listening to the connection
if (!socket_listen($serverSocket)) {
    die("socket_listen() fail: " . socket_strerror(socket_last_error($serverSocket)) . "\n");
}

echo "The server is started,Listening {$address}:{$port} ...\n";

// Receive client connections
$clientSocket = socket_accept($serverSocket);
if ($clientSocket === false) {
    echo "socket_accept() fail: " . socket_strerror(socket_last_error($serverSocket)) . "\n";
} else {
    // Send a welcome message to the client
    $welcome = "Welcome to connect m66.net server\n";
    socket_write($clientSocket, $welcome, strlen($welcome));

    // Read client data
    $input = socket_read($clientSocket, 1024);
    echo "Received a client message: " . trim($input) . "\n";

    // Respond to the client
    $response = "You sent it: " . $input;
    socket_write($clientSocket, $response, strlen($response));

    // Close client connection
    socket_close($clientSocket);
}

// 关闭server socket
socket_close($serverSocket);
?>

4. Things to note

  1. Permissions Issue : Administrator permissions may be required when listening for low ports (such as 80 or 443).

  2. Timeout processing : socket_accept() is a blocking function. If you need non-blocking mode, you can use socket_set_nonblock() .

  3. Security : Appropriate error handling and logging mechanisms should be added in production environments to prevent malicious connections.

  4. Multi-client support : This example only handles one client connection. If you need to handle multiple clients at the same time, you can combine pcntl_fork() or multi-threaded extension.

5. Summary

socket_accept() is an indispensable link in PHP network communication, which enables the server to extract a client connection from the listening queue and perform subsequent processing. Through the explanation and examples of this article, you should be able to master the basic socket server construction process and use socket_accept() to realize the reception and interaction functions of client connections.