An example or tutorial that asks the PHP socket to connect multiple back and forth communication
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
------to solve the idea----------------------
You are not in the loop Socket_close//Close the connection is not OK?
------to solve the idea----------------------
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
------to solve the idea----------------------
if ($msgsock = socket_accept ($sock)) = = = = False)
{
echo "Socket receive failed, reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
Break
}
do {
Echo ' read client message \ n ';
$buf = strlen (Socket_read ($msgsock, 8192));
$talkback = "Client said: $buf \ n";
$talkback = "Server-side said:" Good AH \ n ";
Flush ();
if (false = = = Socket_write ($msgsock, $talkback, strlen ($talkback))) {
echo "Socket write failed, Reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
} else {
Echo ' send success ';
}
Socket_close ($msgsock);
Flush ();
} while (true);