我有一个chatdemo.php,其中包括socket.php套接字文件。在socket.php文件中有一行
socket_bind($master, $address, $port);
其中CCD_ 1和CCD_。
然而,当我把它放到浏览器上时(XAMPP中的apache是)http://localhost/demo/chatdemo.php上面写着:
Warning: socket_bind() [function.socket-bind]: unable to bind address [48]:
Address already in use in /Applications/XAMPP/xamppfiles/htdocs/demo/socket.class.php on line 23
socket_bind() failed
所以,相反,我转到命令行,做了一个
php -q /demo/chatbot.demo.php
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /demo/socket.php on line 21
Server Started : 2012-03-27 17:24:24
Listening on : localhost port 13000
Master socket : Resource id #5
所以问题是:为什么我不能在localhost上的浏览器上运行chatdemo.php(使用XAMPP),而我可以在命令行上使用php(非XAMPP)执行它
您甚至不应该尝试通过web服务器运行服务器(侦听端口并接受连接)。web服务器处理请求,并在必要时调用PHP脚本。您的脚本有30秒的时间来完成它。这是预期的行为。但是当你运行服务器时。故事不同。它全天候运行。想想网络服务器,它全天候运行。当然,您的服务器的运行时间可能比这少得多。但如果您在web服务器下运行它,则每个请求的线程1都会被长时间阻塞。
服务器应单独运行。在您的情况下,它应该从命令行运行。
更远的阅读。
- 客户端-服务器体系结构
- 套接字状态和客户端-服务器模型
对于命令行中的警告,只需根据您的位置更改php.ini
中的date.timezone
设置。我使用
date.timezone = "Asia/Dhaka"