0
我有一個小型的Web服務器腳本。如果我將它設置爲'localhost' - 那麼我無法從外部telnet到該端口。如果我將它設置爲FQDN - 那麼我不能像這樣telnet:'telnet localhost 7777'。在這種情況下命名主機的正確方法是什麼?套接字正確的主機名PHP
$host = 'localhost';
$port = 7777;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
while(1)
{
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
while(trim($input)!="")
{
$msg=$msg.$input;
$input = socket_read($spawn, 1024) or die("Could not read input\n");
}
$webserver = new Server($msg);
$output = $webserver->response();
unset($msg);
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
}
你正在寫整個網絡服務器在PHP?通常情況下,您使用Apache,nginx等實際的網絡服務器,PHP只負責提供內容。 – Thanatos 2012-01-29 01:59:13