In PHP network programming, socket_create() and socket_accept() are two very important functions, especially when building server-side applications based on TCP or UDP. This article will introduce the basic functions and usage of these two functions in detail, and show how they can be used together to implement a simple server-side program.
socket_create() is used to create a socket resource for network communication, which is the starting point of network programming. The function prototype is as follows:
resource socket_create ( int $domain , int $type , int $protocol )
$domain : Specifies the protocol family, commonly used are AF_INET (IPv4), AF_INET6 (IPv6) and AF_UNIX (local communication).
$type : Specify the socket type. Common types are SOCK_STREAM (TCP streaming connection), SOCK_DGRAM (UDP datagram).
$protocol : Specifies the specific protocol. Usually TCP uses SOL_TCP and UDP uses SOL_UDP .
Returns a socket resource when creation is successful, and returns false when failure.
Example:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("create socket fail: " . socket_strerror(socket_last_error()));
}
socket_accept() is used to accept client connections on the socket listened to by the server. Its function prototype is:
resource socket_accept ( resource $socket )
The parameter $socket is the socket created and listened to with socket_create() .
After the call, if there is a client connection request, a new socket resource is returned to communicate with the client; if it fails, false is returned.
To enable the server to listen to client requests and receive connections, the usual process is as follows:
Create a socket
Create a socket using socket_create() .
Bind address and port <br> Use socket_bind() to bind socket to local IP and port.
Listen to socket
Use socket_listen() to listen on the port and prepare to accept the connection.
Accept connection <br> Use socket_accept() to block and wait for the client to connect, and return a new socket after success.
Read and write data <br> Communication with the client via a new socket.
Close the connection <br> Close the socket after the communication is completed.
Here is a complete example that demonstrates how to implement a simple TCP server using these two functions:
<?php
// create TCP socket
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($server === false) {
die("create socket fail: " . socket_strerror(socket_last_error()));
}
// Bind IP and ports
if (!socket_bind($server, "0.0.0.0", 8080)) {
die("Bind socket fail: " . socket_strerror(socket_last_error($server)));
}
// Start monitoring,The most allowed5Line up for connections
if (!socket_listen($server, 5)) {
die("监听fail: " . socket_strerror(socket_last_error($server)));
}
echo "The server is started,Waiting for connection...\n";
while (true) {
// Blocking and waiting for client connection
$client = socket_accept($server);
if ($client === false) {
echo "接收连接fail: " . socket_strerror(socket_last_error($server)) . "\n";
continue;
}
// Read client data
$input = socket_read($client, 1024);
echo "Received client data: $input\n";
// Send a response to the client
$response = "Hello from m66.net server!";
socket_write($client, $response, strlen($response));
// Close client connection
socket_close($client);
}
socket_close($server);
?>
In this example:
The server is bound to port 8080 of all local IP addresses.
Use socket_accept() to receive client connection requests, and return a new socket after success.
Read the data sent by the client and return a simple response.
Close the connection to the client and continue waiting for a new connection.
socket_create() is used to create the underlying communication channel.
socket_accept() is used by the server to receive client connections and return sockets that interact with the client.
The combination of the two is the basis of TCP server-side programming.
In actual applications, errors, concurrent connections and data transmission details are also required.
I hope this article can help you better understand and apply socket_create() and socket_accept() in PHP to easily build socket-based network services.