What is socket? socket is also called & quot; socket & quot;. it is used to describe the IP address and port, and is a communication chain handle. Applications usually send requests to or respond to network requests through & quot; socket & quot;
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
View copy print?
-
- // 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 \ n ";
- // Transmit 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 ";
- }
- // 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 stored ed \ n ");
- }
- }
- Socket_close ($ connection );
- // Close the socket
- Printf ("Closed the socket \ n ");
- }
- ?>
3. socket client. php
View copy print?
-
- // 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 \ n ";
- Break;
- } Else {
- // Message sent from the server
- Echo "Buffer Data:". $ buffer. "\ n ";
- Echo "Writing to Socket \ n ";
- // Write customer information to the channel and send it to the server
- If (! Socket_write ($ socket, "some data \ n ")){
- Echo "Write failed \ n ";
- }
- // 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 \ n response from server was:". $ buffer. "\ n ";
- }
- }
- }
- ?>
4. an image of the communication mechanism (disable the socket automatically when the wait time ends)
Socket interaction process
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 code2. run the socket client[Root @ BlackGhost 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 server was: Information stored Ed3. return to the server[Root @ BlackGhost zhangy] #/usr/local/php/bin/php-a/home/zhangy/www/test2/server. phpInteractive mode enabledSocket connectedsend data to clientWrote to socketBuffer: SOME DATAClosed the socket is purely personal. if you have any mistakes, please advise.