An example or tutorial that asks the PHP socket to connect multiple back-and-forth communications

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