In network programming, PHP is not only used to develop web applications, it can also be used to create low-level network communication programs, such as TCP servers. socket_accept() is one of the important functions in PHP to accept client connections. This article will take you to build a simple TCP server step by step and explain the key points.
socket_accept() is a function in the PHP Socket extension that listens on the server to receive connection requests on an established socket. Once the client requests a connection, socket_accept() returns a new socket instance that is used to communicate with the client, rather than the main listening socket.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
This line of code creates a TCP socket ( SOCK_STREAM ) of IPv4 ( AF_INET ) and the protocol type is TCP ( SOL_TCP ).
if (!socket_bind($socket, '0.0.0.0', 12345)) {
die("socket_bind() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
This step binds the socket to the local IP and port, such as 12345 .
if (!socket_listen($socket, 5)) {
die("socket_listen() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
Start listening to incoming connections, 5 indicates the maximum allowed waiting connection queue length.
while (true) {
echo "Waiting for client connection...\n";
$clientSocket = socket_accept($socket);
if ($clientSocket === false) {
echo "socket_accept() fail: " . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}
$welcomeMsg = "Welcome to m66.net of PHP TCP server!\n";
socket_write($clientSocket, $welcomeMsg, strlen($welcomeMsg));
// Read client information
$input = socket_read($clientSocket, 1024);
echo "Received a client message: " . $input;
// Respond to the client
$response = "You sent it: " . trim($input) . "\n";
socket_write($clientSocket, $response, strlen($response));
// Close client connection
socket_close($clientSocket);
}
In the above code, the server will loop infinitely for the client to connect. Once the connection is established, a welcome message is sent, the client input is read, and it is returned as-is.
Although the above code is an infinite loop server, interrupt mechanism or signal processing should be added in actual use. The code to shut down the server is as follows:
socket_close($socket);
You can use telnet to test this TCP server:
telnet m66.net 12345
If the connection is successful, you will see the welcome message returned by the server.
PHP's socket extension is not enabled by default, ensure sockets are enabled in php.ini .
Please use the command line to run the script. The web environment is not suitable for running such long-connected services.
During actual deployment, functions such as exception handling, logging, and multi-process processing should be added.
Through this article you have learned how to create a simple TCP server using PHP socket_accept() function. Although PHP is not the preferred language for web services, it also provides the necessary tools to meet basic needs. This small server example demonstrates the powerful flexibility of PHP.