PHP socket processing does not come over the data stream, how to avoid (as if blocked) _php tutorial

Source: Internet
Author: User

PHP socket does not handle the data stream, how to avoid (as if blocked)


The PHP socket does not handle the data stream, how to handle it (as if it were blocked)
Requirements: PHP accepts a hardware to send data to port 8888, if received, should socket_send function return "\xfa\x01\x01\xff\xaa\xaa\x00\x01\x00\x00\x00\x00\x00\x01", After the hardware receives the data sent by the Socket_send, it will "drip" a sound, but the problem arises, a hardware is OK, but when multiple hardware at the same time to connect and send data at the same time, the hardware can not be a continuous response (that is, the "drip" sound), that is, can be continuously issued "drip" sound, it is , about a few seconds later, and began to respond, after a while, a few connections on the hardware is the case, I have used the non-blocking mode, or this, to solve the problem, the following post code

PHP Code
   = $MAX _users) {$reject = "Server full.                                   Try again later.\n ";                }//Put the current client connection as Socket_select select $connections [$i] = $newconn;                               Enter the connection resource cache container $writefds [$i] = $newconn;                Not connected properly if ($reject) {$close [$i] = true;                                  } else {echo "Welcome to the PHP Chat server!\n";                }//initializes the cache container for the current connection read content $input [$i] = "";            Continue            }//Client connection $i = (int) $rfd;            Read $tmp = @socket_read ($RFD, Php_normal_read);                if (! $tmp) {//Cannot read content print "Connection closed on socket $i \ n";            Close ($i);    Continue            } $input [$i]. = $tmp;            $tmp = substr ($input [$i],-1);                /*if ($tmp! = "\ r" && $tmp! = "\ n") {//no end of line, more data coming            Continue            }*/$line = Trim ($input [$i]);            $input [$i] = ""; Echo ' Client >> '. $line. "                                                            \ r \ n "; Socket_getpeername ($connections [$i],& $remoteIP,& $remotePort); Echo $remoteIP. " \ r \ n "Echo $remotePort." \ r \ n ";//$data =str_split ($buffer);//print_r ($data); $str =" \xfa\x01\x01\xff\xaa\xaa\x00\x01\x00\x00\x00\x00\x00\ X01 ";                                                                                Socket_send ($connections [$i], $str, strlen ($STR), 0);            } foreach ($writefds as $wfd) {$i = (int) $wfd;        $w = Socket_write ($wfd, "Hello");    }}}function Close ($i) {Global $connections, $input, $close; Socket_shutdown ($connections[$i]);    Socket_close ($connections [$i]);    Unset ($connections [$i]);       Unset ($input [$i]); Unset ($close [$i]); >

http://www.bkjia.com/PHPjc/1023580.html www.bkjia.com true http://www.bkjia.com/PHPjc/1023580.html techarticle PHP Socket processing does not come over the data stream, how to avoid (as if blocked) PHP socket processing data flow, how to handle (as if blocking) requirements: PHP accepts a hardware to 8 ...

  • 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.