PHP example shows the socket communication mechanism
Zhang Ying Published on 2010-04-24
Category: PHP
One, what is a socket?
What is a socket a so-called socket, commonly referred to as a "socket", is used to describe an IP address and port, which is a handle to a communication chain. Applications typically make requests to the network through sockets or answer network requests. Plainly speaking is a communication mechanism. It is similar to the Telephone Customer service department of these parts of banks, telecommunications. When you call, there will be the allocation of a person to answer your question, the Customer service department is equivalent to the server side of the socket, your side of the equivalent of the client, in and before you call the end, if someone is looking to talk with you talking, is not possible, because you are communicating with him, Of course, the telephone switchboard of the customer service department will not be allocated repeatedly.
Let me give you an example of how the socket works. If you're a developer based on an application layer, you don't have to understand the principle, but it's better to know that. PHP API for sockets on the Web. It's OK to use it down.
Second, socket server server.php
View copy print?
- <?php
- Establishing a 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 be monitored
- Socket_listen ($socket); //Listening port
- Initializes a data, and communicates with the client
- $buffer = "Connect";
- while (true) {
- //Accept a socket connection
- $connection = socket_accept ($socket);
- if (! $connection) {
- echo "connect fail";
- }else{
- echo "Socket connected\n";
- //Pass an information data to the client
- if ($buffer! = "") {
- echo "send data to client\n";
- Socket_write ($connection, $buffer. "\ n");
- echo "wrote to Socket\n";
- } Else {
- echo "No data in the buffer\n";
- }
- //Get information from the client
- While ($data = @socket_read ($connection, 1024x768, Php_normal_read)) {
- printf ("Buffer:".) $data. "\ n");
- //Get information to the client for a feedback
- Socket_write ($connection, "Information received\n");
- }
- }
- Socket_close ($connection);
- //Close socket
- printf ("Closed the socket\n");
- }
- ?>
Third, socket client client.php
View copy print?
- <?php
- Establish a SOCET connection for the client
- $socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
- $connection = Socket_connect ($socket, ' 127.0.0.1 ', 10008); //Connect server-side sockets
- while ($buffer = @socket_read ($socket, 1024x768, Php_normal_read)) {
- //The server tells the client that its status
- if (Preg_match ("/not connect/",$buffer)) {
- echo "Don t connect\n";
- Break ;
- } Else {
- //server came to information
- echo "Buffer Data:". $buffer. "\ n";
- echo "Writing to socket\n";
- //write the customer's information to the channel and pass it to the server side
- if (!socket_write ($socket, "SOME data\n")) {
- echo "Write failed\n";
- }
- //The response information given by the server after receiving the message
- While ($buffer = Socket_read ($socket, 1024x768, Php_normal_read)) {
- echo "sent to Server:some data\n response from server is:". $buffer. "\ n";
- }
- }
- }
- ?>
Iv. a picture of the communication mechanism (do not consider the end of the wait time to automatically close the socket)
Socket interaction Process
A brief explanation, before the explanation I want to say, why to use PHP command to execute the server and client, the client you use Fsockopen, so it is accessed on the browser, not at least the connection timeout. Why is this, because when you create a socket, it will constantly listen to the customer there is no need to connect.
Socket communication is established to two socket channels, one is created on the server side, and one is created by the client.
Line Line 1, the server creates a socket channel and places the information in the cache, waiting for the client to connect
Line Line 2, the client creates a socket channel, and connects to the server side, obtains the server side information to communicate, will transmit the information to send the channel
Line Line 3, the server-side obtains information from the client and tells the client that the information I have received. The information to be transmitted is sent to the channel
Line Line 4, client obtains confirmation from server side
To this one communication is completely established, after the data transfer is complete, the server will disconnect the socket communication, the operation is as follows
1, run the socket server side
[Email protected] zhangy]#/usr/local/php/bin/php-a/home/zhangy/www/test2/server.php
Interactive mode Enabled
That's the job.
A, listening on a 10008 port
[Email protected] ~]$ netstat-an |grep LISTEN |grep 10008
TCP 0 0 127.0.0.1:10008 0.0.0.0:* LISTEN
[Email protected] ~]$
b, send the information to the cache
$buffer = "Connect"; See Code 2 above , run the socket client [[email protected] zhangy]#/usr/local/php/bin/php-a/home/zhangy/ Www/test2/client.phpinteractive mode Enabledbuffer data:connectwriting to Socketsent to Server:some Data response from S Erver was:information Received3, back to server [e-mail protected] zhangy]#/usr/local/php/bin/php-a/home/zhangy/www /test2/server.phpinteractive mode enabledsocket connectedsend data to Clientwrote to Socketbuffer:some DATAClosed the SOC Ket about purely personal understanding, if not, please teach the heroes.
PHP example shows the socket communication mechanism