An example of phpsocket persistent connection for multiple back-and-forth communications or an example found on the internet is probably like this process: socket_create & nbsp; connect socket_bind & nbsp; bind socket_listen & nbsp; listen to do & nbsp; {if & nbsp; (socket_accept connection is received) {socket_read & nbsp; read the example or tutorial of receiving a php socket persistent connection for multiple round-trip communication
The example found on the internet is probably a process like this:
Socket_create // Connect
Socket_bind // bind
Socket_listen // listener
Do {
If (received socket_accept connection)
{
Socket_read // read the received information
Socket_write // return Response Information
}
Socket_close // close the connection
} While (true );
After listening, wait for the request. after receiving the request, the system will read the sent information and respond to the request. after the response, the connection will be closed.
(The process I have understood from the example code above may be incorrect)
The problem found after the experiment is: In this example, it seems that the client cannot maintain a persistent connection, because "if the client receives the request, it will read the sent information and then respond, close the connection after the response"
Hope you can give me some advice on the implementation process of php socket persistent connection for multiple round-trip communication, or give a tutorial or an example.
------ Solution ----------------------
You are not in the loop. socket_close // Can you just close the connection?
------ Solution ----------------------
Why should I write the php socket server by myself? using the workerman socket server framework, long links or short links to a configuration field are stable and high in performance. The most important thing is simplicity.
------ Solution ----------------------
If ($ msgsock = socket_accept ($ sock) === false)
{
Echo "failed to receive socket because:". socket_strerror (socket_last_error ($ sock). "\ n ";
Break;
}
Do {
Echo 'read client message \ n ';
$ Buf = strlen (socket_read ($ msgsock, 8192 ));
$ Talkback = "client: $ buf \ n ";
$ Talkback = "Hello \ n" on the server side ";
Flush ();
If (false === socket_write ($ msgsock, $ talkback, strlen ($ talkback ))){
Echo "socket write failed, cause:". socket_strerror (socket_last_error ($ sock). "\ n ";
} Else {
Echo 'sent successfully ';
}
// Socket_close ($ msgsock );
// Flush ();
} While (true );