When using PHP for network programming, Socket is an important mechanism for implementing server-client communication. socket_accept() and socket_read() are key functions for handling client connections and reading messages. This article will explain in detail how to use these two functions to build a simple TCP server program to realize the function of receiving client messages.
First, make sure PHP has sockets extension installed. Use the following command in the command line to check whether it is supported:
php -m | grep sockets
If not enabled, you can uncomment extension=sockets in php.ini and restart the server.
Below is a complete sample code showing how to use socket_accept() and socket_read() to receive messages sent by clients.
<?php
// set up IP and ports
$host = '0.0.0.0';
$port = 8080;
// create Socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
// Bind to the specified IP and ports
if (!socket_bind($socket, $host, $port)) {
die("socket_bind() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
// Start listening to the connection
if (!socket_listen($socket, 5)) {
die("socket_listen() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
echo "The server has started,monitor {$host}:{$port}...\n";
// Infinite loop processing client connections
while (true) {
// Accept client connections
$client = socket_accept($socket);
if ($client === false) {
echo "socket_accept() fail: " . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}
// Read messages from the client
$input = socket_read($client, 1024);
if ($input === false) {
echo "socket_read() fail: " . socket_strerror(socket_last_error($client)) . "\n";
} else {
echo "Received a client message: " . trim($input) . "\n";
// Reply to the client
$response = "The server has received a message: " . $input . "\n";
socket_write($client, $response, strlen($response));
}
// Close client connection
socket_close($client);
}
// Close the main Socket
socket_close($socket);
You can use telnet to test the server or write a simple PHP client as follows:
<?php
$host = 'm66.net';
$port = 8080;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("create客户端 socket fail: " . socket_strerror(socket_last_error()) . "\n");
}
if (!socket_connect($socket, $host, $port)) {
die("连接fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
$message = "Hello,Server side!";
socket_write($socket, $message, strlen($message));
$response = socket_read($socket, 1024);
echo "收到Server side回应: $response\n";
socket_close($socket);
socket_accept() : used to accept connection requests from the client. If there is no client connection, it blocks waiting.
socket_read() : used to read data from the client. It can specify the maximum number of bytes to be read, usually 1024 or more.
socket_write() : used to send data to the client.
The basic TCP communication mechanism can be implemented using socket_accept() and socket_read() . Although PHP is not the most commonly used language for underlying network programming, it still has strong network communication capabilities through Socket extensions. In certain specific scenarios, such as building lightweight services, message listeners, or gateway components, PHP Socket is a simple and practical choice.
With the examples in this article, you have learned how to create a server socket, accept client connections, read client messages, and respond. Combining appropriate error handling and concurrent design, a more complex communication system can be further built.