PHP using the Socket programming example

Source: Internet
Author: User
Tags install php ini php code socket socket connect strlen

  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 () failed because: ". 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 "Connect 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  /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 The code that does not 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/lo     Cal/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.