Phpsocket cannot process data streams. how to avoid this problem (as if it is blocked)

Source: Internet
Author: User
Php socket cannot process data streams. how to handle (seems to be blocked) requirements: php accepts a hardware to send data to port 8888. after receiving the data, the socket_send function returns "xfax01x01xffxaaxaax00x01x00x00x00x00x01 ", after the hardware receives the data sent by socket_send, it will "drip", but the problem arises. The php socket cannot process the data stream, and how to handle it (as if it is blocked)
Requirement: php accepts a hardware to send data to port 8888, the socket_send function should return "\ xFA \ x01 \ x01 \ xFF \ xAA \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 ", after the hardware receives the data sent by socket_send, it will "drip", but the problem arises. one hardware is fine, but when multiple hardware are connected and data is sent at the same time, the hardware will not respond continuously (that is, the sound of a "drop"). That is to say, after a "drop" sound can be sent continuously, it will not sound. after about a few seconds, I started to respond again. after a while, I won't be able to do it again. I have used the non-blocking mode for a few hardware connections. I still want to solve this problem. I will post the code below.

PHP code
  
  = $ MAX_USERS) {$ reject = "Server full. try again later. \ n ";}// set the current client connection to socket_select and select $ connections [$ I] = $ newconn; // enter the connection resource cache container $ writefds [$ I] = $ newconn; // The connection is abnormal if ($ reject) {$ close [$ I] = true ;} else {echo "Welcome to the PHP Chat Server! \ N ";}// initialize the cache container for the content read by the current connection $ input [$ I] =" "; continue ;}// client connection $ I = (int) $ rfd; // read $ tmp = @ socket_read ($ rfd, 14, PHP_NORMAL_READ); if (! $ Tmp) {// The content cannot be read. 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 \ 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]);}?>


------ Solution --------------------
Non-blocking mode

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.