Solution: for PHPsocket problems, the following test code shows that the environment I set up locally with appserv is correctly executed. You can also use a client to connect to and execute the command. & Lt ;? Php & nbsp; // Reduce & nbsp; errorserror_reporting (~ E_WARNING); & nbsp; // C: PHP socket problem
The following test code shows the local environment built with appserv, and the execution is correct. You can also use a client to connect to and execute the command.
//Reduce errors
error_reporting(~E_WARNING);
//Create a UDP socket
if(!($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 address
if( !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 clients
while(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);
?>
However, after I put it on the vps, it shows that the port is opened and cannot be opened again (when the script is executed the second time, an error message is returned, and the port is occupied ). I scanned the port and found that Port 9999 is indeed an open UDP port, but the client cannot complete the communication (maybe it cannot be sent or received, who knows ...)
The following is the result of phpinfo, indicating that socket support is enabled. (Too long, the socket found after ctr + f is here)
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 Transportstcp, udp, unix, udg, ssl, sslv3, sslv2, tls
sockets
Sockets Supportenabled
default_socket_timeout6060
Well, who has experience? thank you ~
------ Solution --------------------
Change the port number
The opened Port cannot be opened again.
------ Solution --------------------
Look at the firewall or something.
------ Solution --------------------
9999 is an opened port, right? but do you have port 9999 that is occupied by no other program? if it is not occupied by other programs, you may consider setting a firewall.
Otherwise, use a new port or close the program that uses port 9999.
You can also change the program to another port.
In short, a port can only be mapped to one program.