In network programming, the server needs to be able to accept connection requests from the client. PHP provides a series of socket functions to handle underlying network communication, where socket_accept() is an important function to implement server listening and receiving connections. This article will introduce in detail how to use socket_accept() to receive client connections and demonstrate how to use them through examples.
socket_accept() is a function in PHP that receives a connection from a socket listening queue. The basic syntax is as follows:
socket_accept(Socket $socket): Socket|false
parameter :
$socket : Listening socket created and bound by socket_create() and socket_bind() .
Return value :
Returns a new socket instance when successful, for communication with the client;
Returns false on failure.
Before using socket_accept() , you usually need to complete the following steps:
Create socket: socket_create()
Bind address and port: socket_bind()
Start listening: socket_listen()
Receive connection: socket_accept()
Communication with clients: socket_read() and socket_write()
Close the connection: socket_close()
Here is a complete socket server example showing how to use socket_accept() to receive client connections:
<?php
// Set the address and port
$address = '0.0.0.0';
$port = 12345;
// create socket
$serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($serverSocket === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
// Bind address and port
if (!socket_bind($serverSocket, $address, $port)) {
die("socket_bind() fail: " . socket_strerror(socket_last_error($serverSocket)) . "\n");
}
// Start listening to the connection
if (!socket_listen($serverSocket)) {
die("socket_listen() fail: " . socket_strerror(socket_last_error($serverSocket)) . "\n");
}
echo "The server is started,Listening {$address}:{$port} ...\n";
// Receive client connections
$clientSocket = socket_accept($serverSocket);
if ($clientSocket === false) {
echo "socket_accept() fail: " . socket_strerror(socket_last_error($serverSocket)) . "\n";
} else {
// Send a welcome message to the client
$welcome = "Welcome to connect m66.net server\n";
socket_write($clientSocket, $welcome, strlen($welcome));
// Read client data
$input = socket_read($clientSocket, 1024);
echo "Received a client message: " . trim($input) . "\n";
// Respond to the client
$response = "You sent it: " . $input;
socket_write($clientSocket, $response, strlen($response));
// Close client connection
socket_close($clientSocket);
}
// 关闭server socket
socket_close($serverSocket);
?>
Permissions Issue : Administrator permissions may be required when listening for low ports (such as 80 or 443).
Timeout processing : socket_accept() is a blocking function. If you need non-blocking mode, you can use socket_set_nonblock() .
Security : Appropriate error handling and logging mechanisms should be added in production environments to prevent malicious connections.
Multi-client support : This example only handles one client connection. If you need to handle multiple clients at the same time, you can combine pcntl_fork() or multi-threaded extension.
socket_accept() is an indispensable link in PHP network communication, which enables the server to extract a client connection from the listening queue and perform subsequent processing. Through the explanation and examples of this article, you should be able to master the basic socket server construction process and use socket_accept() to realize the reception and interaction functions of client connections.