Example or tutorial of multiple round-trip communication with phpsocket persistent connection

Source: Internet
Author: User
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 );

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.