求解:PHP socket 的问题
下面这段测试代码,我在本地用appserv搭建的环境,执行无误。而且可以用客户端连上去并执行。
<?php<br /> <br />//Reduce errors<br />error_reporting(~E_WARNING);<br /> <br />//Create a UDP socket<br />if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))<br />{<br /> $errorcode = socket_last_error();<br /> $errormsg = socket_strerror($errorcode);<br /> <br /> die("Couldn't create socket: [$errorcode] $errormsg \n");<br />}<br /> <br />echo "Socket created \n";<br /> <br />// Bind the source address<br />if( !socket_bind($sock, "localhost" , 9999) )<br />{<br /> $errorcode = socket_last_error();<br /> $errormsg = socket_strerror($errorcode);<br /> <br /> die("Could not bind socket : [$errorcode] $errormsg \n");<br />}<br /> <br />echo "Socket bind OK \n";<br /> <br />//Do some communication, this loop can handle multiple clients<br />while(1)<br />{<br /> echo "Waiting for data ... \n";<br /> <br /> //Receive some data<br /> $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);<br /> echo "$remote_ip : $remote_port -- " . $buf;<br /> <br /> //Send back the data to the client<br /> socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);<br />}<br /> <br />socket_close($sock);<br /><br />?>
但是,我放到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
sockets<br /><br />Sockets Support enabled<br />
default_socket_timeout 60 60
额,哪位大牛有过经验,跪谢啊~
------解决方案--------------------
换一个端口号
已打开的端口,就不能重复打开
------解决方案--------------------
看看防火墙什么的
------解决方案--------------------
9999 是已经打开的端口没错 但是你有没有9999这个端口有没有没其它的程序所占用 如果没有被其它的程序所占用那么可能考虑设置一下防火墙
否则的话 就换一个新的端口或者关闭占用9999端口的程序
也可以把那个程序换一个其它的端口
总之一个端口只能映射给一个程序

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
