How does php process multiple tasks at the same time (about phpsocket)

Source: Internet
Author: User
How does php process multiple tasks (phpsocket) at the same time? I want to write a socket to listen to a port now. if there is data, start the while loop. now the problem is solved, at the same time, only one client can connect to this server. when there is a second connection, it cannot be connected. some friends say it is only once, but how should we modify it? I will post my code below. please give it a friend's Guide (for details, it is best to know how php can process multiple tasks at the same time (about php socket)
Now I want to write a socket to listen to a port. if there is data, start the while loop. now the problem arises. at the same time, only one client can connect to this server. when there is a second connection, I cannot connect to it. some friends say that I just got an accept once, but how should I modify it? The following is my code. please give it a friend's Guide (for details, you 'd better modify the code for me)



PHP code
  $ CommonProtocol = getprotobyname ("tcp"); $ socket = socket_create (AF_INET, SOCK_STREAM, $ commonProtocol); @ socket_bind ($ socket, '2017. 168.1.202 ', 8888); @ socket_listen ($ socket); // Initialize the buffer $ buffer = "no data"; while (true) {sleep (1 ); // Accept any connections coming in on this socket $ connection = @ socket_accept ($ socket); printf ("Socket connected \ r \ n "); // Check to see if there is anything in t He buffer if ($ buffer! = "") {Printf ("Something is in the buffer... sending data... \ r \ n "); @ socket_write ($ connection, $ buffer. "\ r \ n"); printf ("Wrote to socket \ r \ n");} else {printf ("No Data in the buffer \ r \ n ");} // Get the input while ($ data = @ socket_read ($ connection, 14, PHP_NORMAL_READ) {if (! Empty ($ data) {$ buffer = $ data; // FILE drive mode/* $ f = fopen (dirname (_ FILE __). '/file/'.date('ymdhis'0000.'.txt', 'w'); fwrite ($ f, $ buffer); fclose ($ f); */socket_set_nonblock ($ connection); socket_getpeername ($ connection, & $ remoteIP, & $ remotePort); echo $ remoteIP. "\ r \ n"; echo $ remotePort. "\ r \ n"; $ data = str_split ($ buffer); print_r ($ data); foreach ($ data as $ v) {echo dechex (ord ($ v )). "\ t" ;}echo "\ r \ n" ;}}@ socket_close ($ connection ); printf ("Closed the socket \ r \ n ");}


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