Maison >développement back-end >tutoriel php >求解:PHP socket 的有关问题
求解: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 />?>
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