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

Source: Internet
Author: User
Tags foreach array continue php code socket client
PHP socket processing does not come over the data flow, how to deal with (as if it is blocked)
Requirements: PHP accepts a hardware to send data to 8888 port, 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 Socket_send, will "drop" a sound, but the problem arises, a hardware is fine, but when more than one hardware connection and send data at the same time, there will be hardware can not be a continuous response (that is, the "drip" sound), that is, to send a continuous "drip" sound, it is not ringing , about a few seconds later, began to respond again, after a while, the hardware on several connections are like this, I have used non-blocking mode, or will do so, to find a solution, the following post code

PHP Code

   -->= $MAX _users) {$reject = ' Server full.                   
                Try again later.\n ";
                ///Place the current client connection as Socket_select select $connections [$i] = $newconn;               
                The input connection resource cache container $writefds [$i] = $newconn;
                The connection is not normal 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) {//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]);


 }?>


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.