用下面的代碼,我可以收到1個請求,並把它寫:PHP插座聽力環
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包偵聽。我需要繼續接收數據包,直到收到退出或任何關閉命令。
我應該如何改變上面的代碼使這個函數成爲一個循環?
如果我通過這個端口發送數據,我得到一個字符,然後停止打印任何東西。我想這是原始海報的問題。 – adrianTNT