PHP socket Communication Mechanism example description and code

Source: Internet
Author: User

PHP socket Communication Mechanism example description and code----What is a socket the so-called socket is also commonly referred to as "socket", which is used to describe the IP address and port, and is a handle to a communication chain. The use of a program typically passes the "socket" request to the network and may be a response to the network request. Plainly speaking is a communication mechanism. It's similar to banking, telecommunications, some parts of the phone service business unit. When you call, there will be set up a person to answer your question, customer service business unit is equivalent to the server side of the socket, your side is equivalent to the user side, before and you call the end, suppose someone is looking to talk with you the conversation is impossible, because you are communicating with him, Of course, the Customer service unit of the telephone changer will not be repeated deployment.


Let me give you an example of how the socket works. Suppose that you are based on the use of layer development people do not have to understand the principle, but can know that is better. The network has a PHP API for sockets. It's OK to use it down.


Socket server server.php//build server-side socket


$tcp = Getprotobyname ("TCP");


$socket = Socket_create (Af_inet, Sock_stream, $tcp);


Socket_bind ($socket, ' 127.0.0.1 ', 10008); Bind the port to listen on


Socket_listen ($socket); Listening port


Initializes a data, and client-side communication


$buffer = "Connect";


while (true) {//accepts a socket join


$connection = Socket_accept ($socket);


if (! $connection) {


echo "Connect fail";


}else{


echo "Socket Connectedn"; Send a message to the client data


if ($buffer! = "") {


echo "Send data to Clientn";


Socket_write ($connection, $buffer. "N");


Echo "wrote to Socketn";


} else {


echo "No data in the Buffern";


}//Get information from client


while ($data = @socket_read ($connection, 1024x768, php_normal_read)) {


printf ("Buffer:".) $data. "N"); Get information to the client a response


Socket_write ($connection, "Information Receivedn");


}


}


Socket_close ($connection); Block socket


printf ("closed the Socketn");


}


?>


Also note that the socket user-side client.php


Building Socet joins on the client side


$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);


$connection = Socket_connect ($socket, ' 127.0.0.1 ', 10008); Join server-side sockets


while ($buffer = @socket_read ($socket, 1024x768, Php_normal_read)) {//The server tells the client that its shape


if (Preg_match ("/not connect/", $buffer)) {


echo "Don t connectn";


Break


} else {//server came with information


echo "Buffer data:". $buffer. "N";


PHP socket Communication Mechanism example description and code

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.