Current Location: Home> Latest Articles> How to build a service using socket_accept() in PHP CLI mode

How to build a service using socket_accept() in PHP CLI mode

M66 2025-05-18

In PHP's CLI (command line interface) mode, the socket_accept() function can be used to handle low-level network connections. This allows us to build a simple TCP server using PHP to accept client requests and send responses. While PHP is not the preferred language for building web services in the traditional sense, it provides enough underlying APIs to allow us to easily experiment or build lightweight services.

Basic concepts

Before establishing a network service, you need to understand the following PHP Socket functions:

Example: Build a simple TCP server

Here is an example of a simple PHP TCP server built using socket_accept() .

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

// Create a socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock === false) {
    die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");
}

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

// Start monitoring
if (!socket_listen($sock, 5)) {
    die("socket_listen() failed: " . socket_strerror(socket_last_error($sock)) . "\n");
}

echo "The server is started,Listen to port $port...\n";

while (true) {
    // Accept a connection
    $client = socket_accept($sock);
    if ($client === false) {
        echo "socket_accept() failed: " . socket_strerror(socket_last_error($sock)) . "\n";
        continue;
    }

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

    // Construct the response content
    $response = "Welcome to visit m66.net Testing services!What you sent is:$input\n";

    // Write back to the client
    socket_write($client, $response, strlen($response));

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

// Close the main socket(In theory, it will never be executed here)
socket_close($sock);

How to run

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

 php server.php

This script will listen to port 9000 and wait for the client to connect. You can use telnet or other TCP client tools to connect to the server:

 telnet 127.0.0.1 9000

After entering any text and pressing, the server will echo the text with a welcome message.

Things to note

  • Please make sure PHP has sockets extension enabled (can be checked via php -m ).

  • The service is single threaded and can only process one connection at a time. If multiple connections need to be processed concurrently, you can use pcntl_fork() or use more professional services such as Swoole.

  • Do not use such a simple implementation in production environments, as it does not have any security and fault tolerance.

Application scenarios

Although PHP is not the main language for network service development, such socket services can be used for:

  • Simple debugging tool

  • LAN data transmission and reception services

  • Prototyping of custom protocols

  • Exchange of data with non-Web systems (such as hardware devices)