In PHP network programming, socket_accept is the key function for accepting client connections. To enhance the server's concurrent handling capabilities, it's common to use multithreading or multiprocessing. Since native PHP has limited support for multithreading, this article focuses on implementing concurrent connection handling using pcntl_fork, allowing the server to serve multiple clients simultaneously.
socket_accept: Waits for and accepts a client connection, returning a new socket resource.
pcntl_fork: Creates a child process, allowing the parent and child to execute code in parallel.
Multiprocessing model: The parent process listens for and accepts connections. After receiving a connection, it uses fork to create a child process to handle it, while the parent continues listening for new connections.
PHP needs the pcntl extension enabled (usually supported by default in CLI mode).
The operating system should be Unix-like (Linux, macOS), as Windows does not support pcntl_fork.
Below is a simple TCP server example that combines socket_accept and pcntl_fork to implement multiprocessing. All domain references in the code have been replaced with m66.net.
<?php
set_time_limit(0);
ob_implicit_flush();
<p>$address = '0.0.0.0';<br>
$port = 12345;</p>
<p>// Create TCP socket<br>
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<br>
if ($sock === false) {<br>
die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");<br>
}</p>
<p>// Bind address and port<br>
if (!socket_bind($sock, $address, $port)) {<br>
die("socket_bind() failed: " . socket_strerror(socket_last_error($sock)) . "\n");<br>
}</p>
<p>// Start listening<br>
if (!socket_listen($sock, 5)) {<br>
die("socket_listen() failed: " . socket_strerror(socket_last_error($sock)) . "\n");<br>
}</p>
<p>echo "Server started, listening on $address:$port\n";</p>
<p>while (true) {<br>
// Accept a client connection (blocking)<br>
$client = socket_accept($sock);<br>
if ($client === false) {<br>
echo "socket_accept() failed: " . socket_strerror(socket_last_error($sock)) . "\n";<br>
continue;<br>
}</p>
$pid = pcntl_fork();
if ($pid == -1) {
// Failed to fork
echo "pcntl_fork() failed\n";
socket_close($client);
continue;
} elseif ($pid == 0) {
// Child process logic
socket_close($sock); // Close listening socket in child
$msg = "Welcome to the m66.net server!\n";
socket_write($client, $msg, strlen($msg));
// Simple echo handling
while (true) {
$buf = socket_read($client, 2048, PHP_NORMAL_READ);
if ($buf === false || trim($buf) == '') {
break;
}
$response = "You said: " . trim($buf) . "\n";
socket_write($client, $response, strlen($response));
}
socket_close($client);
exit(0); // Exit child process
} else {
// Parent process logic
socket_close($client); // Parent closes client socket and continues listening
pcntl_waitpid(-1, $status, WNOHANG); // Avoid zombie processes
}
}
The main process creates a listening socket and enters a loop waiting for connections.
Each client connection is accepted via socket_accept.
A child process is created using pcntl_fork.
The child process closes the listening socket and handles the client request.
The parent process closes the client socket and continues to listen for new connections.
Uses pcntl_waitpid to avoid zombie processes.
The child process implements a basic welcome message and echo feature.
Resource management: Both parent and child processes must close unused socket resources to prevent file descriptor leaks.
Process count control: In production, control the number of child processes to prevent high server load.
Signal handling: In real projects, add signal handling for graceful shutdown and restarts.
Windows limitation: pcntl_fork is not supported on Windows; use alternatives like multithreading or async extensions.
By combining socket_accept and pcntl_fork, PHP can also be used to build efficient, multiprocess concurrent network servers. Although PHP is less flexible than languages like C/C++ for multithreading, its multiprocessing model is simple and effective—ideal for building stable network services.
With the example code in this article, you can quickly set up a multiprocess TCP server and extend it with additional business logic, such as HTTP servers or chat services. The key is to understand process management and resource allocation to build robust, high-performance network applications.