The examples that are found on the web are probably such processes:
Socket_create//Connection
Socket_bind//Binding
Socket_listen//monitoring
do {
if (Received socket_accept connection)
{
Socket_read//Read the received information
Socket_write//Return response information
}
Socket_close//Close connection
} while (true);
After listening, wait for the request, if received after the request will read the message and then respond, after the response to close the connection.
(The process that I understand from the example code above, maybe I also have the wrong place)
After experimenting with this example, the problem is that this example does not seem to allow the client to keep a long connection because he "reads the message and responds after receiving the request, then closes the connection after the response."
Want to have a hero to guide the PHP socket long connection to the implementation process of multiple back and forth communication, or give a tutorial or give an example
Reply to discussion (solution)
You are not in the loop Socket_close//Close the connection is not OK?
Try not to socket_close//close the connection in the loop, but still can't receive the second message from the client, related code:
do {if ($msgsock = socket_accept ($sock) = = = = False) {//Get a link echo "socket received failed because:". Socket_strerror (Socket_last_error ($ Sock)). "\ n"; Echo ' read client message \ n '; $buf = strlen (Socket_read ($msgsock, 8192)); $talkback = "client says: $buf \ n"; $talkback = "Server-side said:" OK \ n "; Flush () if (false = = = Socket_write ($msgsock, $talkback, strlen ($talkback))) {echo "socket write failed, Reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";} else {echo ' sent successfully ';} Socket_close ($msgsock);//flush ();} while (true);
PHP Socket Server Why do you write it, with Workerman Socket server framework Ah, long links or short links a configuration field of things, stable, high performance, the most important is simple
if ($msgsock = socket_accept ($sock)) = = = False) {echo "socket received failed, reason:". Socket_strerror (Socket_last_error ($sock)). " \ n "; break;} Do {echo ' read client message \ n '; $buf = strlen (Socket_read ($msgsock, 8192)); $talkback = "client says: $buf \ n"; $talkback = "Server-side said:" Yes \ n "; Flush (); if (false = = = Socket_write ($msgsock, $talkback, strlen ($talkback))) {echo "socket write failed, Reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";} else {echo ' sent successfully ';} Socket_close ($msgsock);//flush ();} while (true);
You can send another request immediately after you send another one.