This article mainly introduces PHP using socket Programming example, we refer to the use of the bar
2 php Test files server.php code as follows: <?php//phpinfo (); Ensure that the client is not timed out set_time_limit (0); $IP = ' 127.0.0.1 '; $port = 1935; /* +------------------------------- * @socket communication process +------------------------- ------ * @socket_create * @socket_bind * @socket_listen * @socket_accept * @socket_read * @socket_write * @ Socket_close +-------------------------------- */ /*---------------- The following operations are in the manual. -------------------*/if ($sock = Socket_create (AF_INET,SOCK_STREAM,SOL_TCP) < 0) { echo " Socket_create () The reason for the failure is: ". Socket_strerror ($sock)." n "; } if (($ret = Socket_bind ($sock, $ip, $port)) < 0) { echo ' Socket_bind () failed because of: ". Socket_strerror ($re T). " n "; } if (($ret = Socket_listen ($sock, 4)) < 0) { echo socket_listen () The reason for the failure is: ". Socket_strerror ($ret)." n "; } $count = 0; Do { if ($msgsock = socket_accept ($sock)) < 0) { Echo socket_accept ( ) Failed:reason: ". Socket_strerror ($msgsock). "N"; break; } else { //Sent to client $msg = "Test success!" n "; Socket_write ($msgsock, $msg, strlen ($msg)); echo "Test succeeded Ah N"; $BUF = Socket_read ($msgsock, 8192); $talkback = "Received information: $BUFN"; echo $talkback; if (+ + $count >= 5) { break; &N Bsp }; } //echo $buf; Socket_close ($msgsock); } while (true); Socket_close ($sock);?> client.php code as follows: <?php error_reporting (E_all); Set_time_limit (0); echo "<h2>tcp/ip connection</h2>n"; $port = 1935; $ip = "127.0.0.1"; /* +------------------------------- * @socket connect the entire process +------------------------- ------ * @socket_create * @socket_connect * @socket_write * @socket_read * @socket_close +-------------------------------- */ $socket = Socket_create (Af_inet, Sock_stream, sol_tcp); if ($socket < 0) { echo "Socket_create () Failed:reason:". Socket_strerror ($socket). "N"; }else { echo "OK.N";} echo "Attempts to connect ' $ip ' port ' $port ' ... n"; $result = Socket_connect ($socket, $ip, $port); if ($result < 0) { echo "Socket_connect () Failed.nreason: ($result)". Socket_strerror ($result). "N"; }else { echo "Connection OKn "; } $in = "Horn"; $in. = "BLOODRN"; $out = '; if (!socket_write ($socket, $in, strlen ($in)) { echo socket_write () Failed:reason: ". Socket_strerror ($socket). "N"; }else { echo "sent to server information successfully!" n "; echo "sent the content: <font color= ' red ' > $in </font> <br>"; } while ($out = Socket_read ($socket, 8192)) { echo "Receive server return information successfully!" n "; echo "accepted:", $out;} echo "Close SOCKET...N"; Socket_close ($socket); echo "Close Okn";?> Listening code is as follows: #/usr/local/php/bin/php &NBSP;/USR/LOCAL/APACHE2/HTDOCS/S erver.php request code is as follows: # /usr/local/php/bin/php/usr/local/apache2/htdocs/client.php default PHP is not The code to open the socket is as follows: #cd./ext/sockets/#/usr/local/php/bin/phpize #/configure--enable-sockets--with-php-config=/usr/ Local/php/bin/php-config # make # make install php.ini modify configuration Add code as follows: extension=sockets.so Restart the Apache code as follows: #/usr/loc Al/apache2/bin/apachectl Restart configuration changes under Windows PHP.ini code is as follows: Extension=php_sockets.dll reboot Apache cmd down to 2 PHP installation directory The code is as follows: C:/php/php.exe f:web/server.php C:/php/php.exe f:web/client.php