Phpsocket communication mechanism example description and code _ PHP Tutorial

Source: Internet
Author: User
Phpsocket communication mechanism instance description and code. Phpsocket communication mechanism instance description and code php Tutorial socket communication mechanism instance description and code 1, socket what is socket commonly referred to as socket, describes the php socket communication mechanism instance description and code

Php Tutorial socket communication mechanism instance description and code

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.

2. socket server. 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 connectedn ";
// Transmit an information data to the client
If ($ buffer! = ""){
Echo "send data to clientn ";
Socket_write ($ connection, $ buffer. "n ");
Echo "wrote to socketn ";
} Else {
Echo "no data in the buffern ";
}
// Obtain information from the client
While ($ data = @ socket_read ($ connection, 1024, php_normal_read )){
Printf ("buffer:". $ data. "n ");
// Obtain information and send a feedback to the client
Socket_write ($ connection, "information receivedn ");
}
}

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

3. socket client. 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 connectn ";
Break;
} Else {
// Message sent from the server
Echo "buffer data:". $ buffer. "n ";

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

}
}

?>

4. an image of the communication mechanism (disable the 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


[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. listening for Port 10008
[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. $ buffer = "connect"; check the above code.

2. run the socket client

[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

[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


Http://www.bkjia.com/PHPjc/444905.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444905.htmlTechArticlephp socket communication mechanism instance description and code php Tutorial socket communication mechanism instance description and code 1, socket is what is socket so-called socket is also known as socket, used to describe...

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.