In PHP, using Socket extensions, we can implement network communication functions very flexibly, and even use it to create a basic network server. This article will focus on how to use socket_listen() and socket_accept() functions to build a simple TCP server.
socket_listen()
This function is used to listen for a socket resource with an IP and port bound to wait for the client connection request.
socket_accept()
This function is used to accept an incoming client connection request and returns a new socket resource for communicating with the client.
Create socket resource ( socket_create )
Bind IP and port ( socket_bind )
Listen port ( socket_listen )
Looping and accepting client connections ( socket_accept )
Read and write data and complete communication
Close client and server sockets
<?php
// createTCP socket(AF_INETProtocol family,Streaming)
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($server === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
// BindIPand ports
$address = '0.0.0.0'; // Listen to all network interfaces
$port = 8080;
if (!socket_bind($server, $address, $port)) {
die("socket_bind() fail: " . socket_strerror(socket_last_error($server)) . "\n");
}
// Start monitoring,The queue length that allows waiting for connection is5
if (!socket_listen($server, 5)) {
die("socket_listen() fail: " . socket_strerror(socket_last_error($server)) . "\n");
}
echo "The server is started,monitor {$address}:{$port}\n";
while (true) {
// Blocking and waiting for client connection
$client = socket_accept($server);
if ($client === false) {
echo "socket_accept() fail: " . socket_strerror(socket_last_error($server)) . "\n";
continue;
}
// Read data from the client,Maximum read1024byte
$input = socket_read($client, 1024);
if ($input === false) {
echo "socket_read() fail: " . socket_strerror(socket_last_error($client)) . "\n";
socket_close($client);
continue;
}
echo "Received client data: $input\n";
// Processing data - Here is a simple reply to the client message
$response = "Hello,Client!The data you send is:" . trim($input) . "\n";
socket_write($client, $response, strlen($response));
// 关闭Client连接
socket_close($client);
}
// Shut down the serversocket(Usually I won't go here)
socket_close($server);
?>
In actual use, you can extend the read and write logic according to your needs, such as implementing a complete protocol parser, or processing multiple clients asynchronously.
The listener's IP can be set to a specific address, or listen for all available network interfaces using '0.0.0.0' .
The port number should be avoided using the system to retain ports, and generally, ports above 1024 should be selected.
The above code is a single-threaded server that is synchronously blocked, suitable for beginner understanding.
Through socket_listen() and socket_accept() , PHP can build TCP server programs very directly to realize basic network communication functions. Although PHP is not a language designed specifically for high-performance servers, it is still very practical to use it to quickly prototype small services or make some simple network interaction applications.