次のコードでは、1 つのリクエストを受け取り、それを記述できます。
function listen()
{
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = 'XX.XX.XX.XXX';
$port = XXXX;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
$bind = socket_bind($sock, $address, $port);
// Start listening for connections
socket_listen($sock);
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
$input = socket_read($client, 2024);
// Strip all white spaces from input
echo $input;
// Close the master sockets
$close = socket_close($sock);
var_dump($close);
}
listen();
ただし、1 パケットを受信すると、自動的にリッスンを終了します。exit または close コマンドが受信されるまで、パケットを受信し続ける必要があります。
この関数をループにするには、上記のコードをどのように変更すればよいですか?