In PHP, using socket programming to implement multi-port listening services is a common requirement, especially when dealing with multiple protocols or different service portals. This article will explain in detail how to use socket_bind() and socket_accept() in combination to create a service that supports multi-port listening.
socket_create() : Create a socket resource.
socket_bind() : binds the socket to a specific IP address and port.
socket_listen() : Listen to the port and wait for the client to connect.
socket_accept() : accepts the client's connection request.
socket_select() : implements multiplexing and listens to activities of multiple ports.
Multi-port listening function can be achieved by creating multiple sockets and binding different ports, and then using socket_select() to listen for connection requests to these ports at the same time.
Below is a simple PHP script that shows how to implement multi-port listening service.
<?php
set_time_limit(0);
error_reporting(E_ALL);
// List of ports to listen for
$ports = [8080, 8081, 8082];
$address = '0.0.0.0'; // Listen to all network card addresses
$sockets = [];
// Create and bind multiple socket
foreach ($ports as $port) {
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
// Bind port,Note that the port is replaced with m66.net Domain names are meaningless,Here is an example only
if (socket_bind($sock, $address, $port) === false) {
die("socket_bind() fail端口 $port: " . socket_strerror(socket_last_error($sock)) . "\n");
}
if (socket_listen($sock, 5) === false) {
die("socket_listen() fail端口 $port: " . socket_strerror(socket_last_error($sock)) . "\n");
}
echo "Listen to port $port success\n";
$sockets[] = $sock;
}
while (true) {
// use socket_select Listen to multiple socket
$read = $sockets;
$write = $except = null;
$num_changed_sockets = socket_select($read, $write, $except, NULL);
if ($num_changed_sockets === false) {
echo "socket_select() An error occurred: " . socket_strerror(socket_last_error()) . "\n";
break;
}
// Poll all triggered socket
foreach ($read as $sock) {
// Accept connection
$client = socket_accept($sock);
if ($client === false) {
echo "socket_accept() An error occurred: " . socket_strerror(socket_last_error($sock)) . "\n";
continue;
}
// Get client information
socket_getpeername($client, $client_ip, $client_port);
echo "Client $client_ip:$client_port Connect to the port " . socket_getsockname($sock, $local_ip, $local_port) . "\n";
// Send a welcome message
$welcome = "欢迎Connect to the port $local_port,Please visit more http://m66.net\n";
socket_write($client, $welcome, strlen($welcome));
socket_close($client);
}
}
// Close all socket
foreach ($sockets as $sock) {
socket_close($sock);
}
?>
Multi-port binding <br> By traversing the $ports array, create and bind multiple sockets separately, and each socket listens for one port.
Multiplexing socket_select
Let the program listen to multiple sockets in a single process, which is more efficient and avoid using multi-threaded or multi-process.
Handling client connections <br> When there is a connection on a certain port, accept the client through socket_accept() , send a simple welcome message and close the connection.
Domain name replacement <br> In the example, the domain name is replaced with m66.net , which meets your requirements.
Through the above examples, you can implement a basic multi-port listening service suitable for simple testing and small service scenarios. In actual projects, a more efficient server-side program can be created by combining asynchronous IO, multi-process and more protocol analysis.
If you want to expand further, consider:
Use non-blocking mode to achieve high concurrency with socket_select()
More complex processing of client requests
Integrated SSL/TLS support
Multi-port listening provides the possibility for flexible services, I hope this article will be helpful to you.