Details about socket communication mechanism instances in php

Source: Internet
Author: User

Details about socket communication mechanism instances in php

This article mainly introduces the socket communication mechanism in php, describes the principle of the socket communication mechanism, and analyzes the usage of the socket communication mechanism in detail in the form of an instance. For more information, see

 

 

This article describes the socket communication mechanism and usage in php. Share it with you for your reference. The specific analysis is as follows:

I. What is socket?

What is socket? socket is also called socket. It is used to describe IP addresses and ports and is a communication chain handle. Applications usually send requests to or respond to network requests through "Sockets. To put it bluntly, it is a communication mechanism. It is similar to the telephone customer service departments of banks and China Telecom. When you make a call, there will be someone there to answer your questions. The customer service department is equivalent to the socket server, and your side is equivalent to the client. Before the call ends, it is impossible for someone to talk to you, because you are communicating with him. Of course, the telephone switches of the customer service department will not be allocated repeatedly.

The following example shows how the socket works. If you are a developer based on the application layer, you do not have to understand the principle, but it is better to know it. Php APIs for socket are available on the Internet. And then use it.

Ii. socket server. php

The Code is as follows:

<? Php
// Create a server socket
$ Tcp = getprotobyname ("tcp ");
$ Socket = socket_create (af_inet, sock_stream, $ tcp );
Socket_bind ($ socket, '127. 0.0.1 ', 127); // bind the port to be listened on
Socket_listen ($ socket); // listening port

// Initialize a data file and communicate with the client
$ Buffer = "connect ";
While (true ){
// Accept a socket connection
$ Connection = socket_accept ($ socket );
If (! $ Connection ){
Echo "connect fail ";
} Else {
Echo "socket connected ";
// Transmit an information data to the client
If ($ buffer! = ""){
Echo "send data to client ";
Socket_write ($ connection, $ buffer ."");
Echo "wrote to socket ";
} Else {
Echo "no data in the buffer ";
}
// Obtain information from the client
While ($ data = @ socket_read ($ connection, 1024, php_normal_read )){
Printf ("buffer:". $ data ."");
// Obtain information and send a feedback to the client
Socket_write ($ connection, "information already ed ");
}
}

Socket_close ($ connection );
// Close the socket
Printf ("closed the socket ");
}
?>


3. socket client. php

The Code is as follows:

<? Php
// Establish the socet connection of the Client
$ Socket = socket_create (af_inet, sock_stream, sol_tcp );
$ Connection = socket_connect ($ socket, '192. 0.0.1 ', 127); // connect to the server socket

While ($ buffer = @ socket_read ($ socket, 1024, php_normal_read )){
// The server tells the client about its status.
If (preg_match ("/not connect/", $ buffer )){
Echo "don't connect ";
Break;
} Else {
// Message sent from the server
Echo "buffer data:". $ buffer ."";

Echo "writing to socket ";
// Write customer information to the channel and send it to the server
If (! Socket_write ($ socket, "some data ")){
Echo "write failed ";
}
// Response information provided by the server after receiving the message
While ($ buffer = socket_read ($ socket, 1024, php_normal_read )){
Echo "sent to server: some data response from server was:". $ buffer ."";
}

}
}
?>


4. An image of the communication mechanism (disable socket automatically when the wait time ends)

 

Let me briefly explain why php commands are used to execute servers and clients. You can use fsockopen to access the client on the browser, so that the connection times out at least. Why? When you create a socket, it constantly monitors whether the customer wants to connect.

The establishment of socket communication requires two socket channels, one created on the server side and the other created on the client side.

Line 1, the server creates a socket channel, and stores the information in the cache, waiting for the client to connect

Line 2: the client creates a socket channel, connects to the server, obtains the server information for communication, and sends the information to be transmitted to the channel.

Line 3: The server obtains information from the client and tells the client that I have received the information. The information to be transmitted is sent to the channel.

Line 4. The client obtains confirmation information from the server.

This communication is completely established. After the data transmission is complete, the server will disconnect the socket communication. The specific operations are as follows.

1. Run the socket server

The Code is as follows:

[Root @ blackghost zhangy] #/usr/local/php/bin/php-a/home/zhangy/www/test2/server. php
Interactive mode enabled


What have you done with this?

 

A listens to a port 10008

The Code is as follows:

[Zhangy @ blackghost ~] $ Netstat-an | grep listen | grep 10008

 

Tcp 0 0 127.0.0.1: 10008 0.0.0.0: * listen

[Zhangy @ blackghost ~] $

 

B. Send the information to the cache.

The Code is as follows:

$ Buffer = "connect ";

View the above Code

 

2. Run the socket Client

The Code is as follows:

[Root @ blackghost zhangy] #/usr/local/php/bin/php-a/home/zhangy/www/test2/client. php
Interactive mode enabled

Buffer data: connect

Writing to socket
Sent to server: some data
Response from server was: information received ed


3. Return to the server

The Code is as follows:

[Root @ blackghost zhangy] #/usr/local/php/bin/php-a/home/zhangy/www/test2/server. php
Interactive mode enabled

Socket connected
Send data to client
Wrote to socket
Buffer: some data

Closed the socket

 

I hope this article will help you with php programming.

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.