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