求解:PHP socket 的问题

php中文网
发布: 2016-06-23 14:00:30
原创
1271人浏览过

下面这段测试代码,我在本地用appserv搭建的环境,执行无误。而且可以用客户端连上去并执行。

<?php //Reduce errorserror_reporting(~E_WARNING); //Create a UDP socketif(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))){    $errorcode = socket_last_error();    $errormsg = socket_strerror($errorcode);         die("Couldn't create socket: [$errorcode] $errormsg \n");} echo "Socket created \n"; // Bind the source addressif( !socket_bind($sock, "localhost" , 9999) ){    $errorcode = socket_last_error();    $errormsg = socket_strerror($errorcode);         die("Could not bind socket : [$errorcode] $errormsg \n");} echo "Socket bind OK \n"; //Do some communication, this loop can handle multiple clientswhile(1){    echo "Waiting for data ... \n";         //Receive some data    $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);    echo "$remote_ip : $remote_port -- " . $buf;         //Send back the data to the client    socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);} socket_close($sock);?>
登录后复制


但是,我放到vps上之后,它显示该端口是打开的,而且无法再次打开(第二遍执行这个脚本的时候,返回错误信息,端口已被占用)。我扫描端口发现9999确实已经是一个打开的UDP端口,但是,用客户端连得上去却没法完成通信(或许不可以发,或许不可以收,who knows。。。)

下面是phpinfo的结果,显示socket支持是打开的。(太长了,ctr+f后找到的socket都在这里了)
Configure Command	 './configure' '--prefix=/usr/local/php5' '--enable-force-cgi-redirect' '--enable-fastcgi' '--with-config-file-path=/usr/local/etc/php5/cgi' '--with-curl=/usr/local/lib' '--with-gd' '--with-gettext' '--with-jpeg-dir=/usr/local/lib' '--with-freetype-dir=/usr/local/lib' '--with-kerberos' '--with-openssl' '--with-mcrypt' '--with-mhash' '--with-mysql=/usr' '--with-mysqli=/usr/bin/mysql_config' '--with-pcre-regex=/usr/local' '--with-pdo-mysql=/usr' '--with-pear' '--with-png-dir=/usr/local/lib' '--with-xsl' '--with-zlib' '--with-zlib-dir=/usr/local/lib' '--enable-zip' '--enable-gd-native-ttf' '--with-iconv=/usr/local' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-mbstring'
登录后复制

Registered Stream Socket Transports	tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
登录后复制

socketsSockets Support	enabled
登录后复制

default_socket_timeout	60	60
登录后复制


额,哪位大牛有过经验,跪谢啊~

AI建筑知识问答
AI建筑知识问答

用人工智能ChatGPT帮你解答所有建筑问题

AI建筑知识问答 22
查看详情 AI建筑知识问答

回复讨论(解决方案)

换一个端口号

已打开的端口,就不能重复打开

我把localhost手动改成了服务器IP,然后加上了set_time_limit(2);

执行的结果和上面一样。扫描显示端口已打开,但是客户端无法通信。

不过,我很快看到了这个:

The server encountered an internal error or misconfiguration and was unable to complete your request
登录后复制


换一个端口号

已打开的端口,就不能重复打开
恩,每次都有换的。

The server encountered an internal error or misconfiguration and was unable to complete your request
服务器遇到一个内部错误或配置错误,无法完成您的请求

The server encountered an internal error or misconfiguration and was unable to complete your request
服务器遇到一个内部错误或配置错误,无法完成您的请求

有什么建议没?比如查log,或者是怎么尝试?

看看防火墙什么的

9999 是已经打开的端口没错 但是你有没有9999这个端口有没有没其它的程序所占用 如果没有被其它的程序所占用那么可能考虑设置一下防火墙

否则的话 就换一个新的端口或者关闭占用9999端口的程序
也可以把那个程序换一个其它的端口

总之一个端口只能映射给一个程序

好吧,最后尝试无果,VPS上的防火墙也没法动。就这样吧。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号