Under what circumstances would we use such a high-level thing? Especially about the flow. Under what circumstances will the flow of such things be used? Establishing a long connection does not mean that PHP is not allowed??? So what are some of the things that PHP is used for?
Reply content:
Under what circumstances would we use such a high-level thing? Especially about the flow. Under what circumstances will the flow of such things be used? Establishing a long connection does not mean that PHP is not allowed??? So what are some of the things that PHP is used for?
PHP can not use long connections, mainly because the language is not resident memory (unless you use PHP at the command line), you request a PHP write page, and then refresh, all the local variables generated during the last request are marked as recycled. Consider that if you use a long connection, you are sure to reuse the socket handle that was used to create the connection, and it is because of this mechanism that it is impossible to save the socket handle.
I don't know what you mean by stream? Binary stream words to use a lot of it, file transfer is basically the binary transmission of the bar, in addition I remember that the transmission of the Open interface is also used binary. I'm not very clear about this question, just a little bit more on the table.
What is an HTTP long connection?
I think you must understand the wrong long connection is something. When it comes to long connections we have to talk about short connections. The HTTP connection is a stateless connection, and after the client and server have established a connection in the three handshake, the client requests the data and the server closes the connection after the data is returned, which is a short connection. Based on the principle of short connection, we use Ajax to constantly request to the server whether there is new data (that is, Ajax pull) behavior is polling . The advantage of this method is simple, the disadvantage of course is also obvious, resulting in a considerable amount of unnecessary waste of resources and server pressure.
In order to solve these problems, a modified version-long connection based on the short connection method appears. The method of long connection is also very simple, client and server in three handshake to establish a connection, client request data, when the server side found no new data when not immediately return, but hold the connection (that is, Keep Alive), with When the new data return/connection times out , the data close connection is returned. Based on the principle of long connection, when the client receives the data return (that is, the connection disconnects) we immediately initiate a new connection to repeat the above process is what we call long polling. The advantage of long connections over short connections is that the number of client and server requests is greatly reduced. As we all know, an HTTP connection needs to undergo three handshake this complex process, the speed of light from the United States through the submarine optical fiber to reach China also need about 50ms time, not to mention also go through three times! So the benefits of this approach are obvious, but there is a bad side to the good. Long connection due to the need for the server hold the connection, when the client requests more time will inevitably cause the server + + pressure, this pressure is not resolved.
According to the question I say the server side hold to connect this thing. It is well known that PHP will return the content to the CGI after the page script is executed and then to the Web server to return to the client side. So hold the connection this is very simple, write a dead loop so that the script has been running to hold the connection. PHP for this implementation is relatively simple, I write a semantic demo here for reference only:
0 ) { echo json_encode($data); break; } sleep(5); } while(true);?>
When using a long connection, remember that when the client initiates the request, it is best to set a connection timeout time, when the link is broken after the time elapses, the goal of controlling the server pressure can be achieved by shortening the time-out period, when the time-out is too small, it is basically no different from polling.
The SOCKET is what ghosts and the most beautiful language can do anything!
Long connection and socket is nothing to do, now we say socket. Socket in the OSI Network layer Seven protocol is between the application layer and the transport layer of things, can be done based on TCP, UDP, RAWIP and link layer socket (from the network chapter). Specific about the HTTP link and the difference between the socket can look at my earlier citation, there is a more detailed description. But simply, sockets can simulate the link to the application layer that is based on TCP/UDP.
layers in the OSI |
function |
TCP/IP protocol family |
Application Layer |
File transfer, email, file service, virtual terminal |
Tftp,http,snmp,ftp,smtp,dns,telnet |
Presentation Layer |
Data formatting, code conversion, data encryption |
No protocol. |
Session Layer |
To release or establish contact with another contact. |
No protocol. |
Transport Layer |
Provides an end-to-end interface |
Tcp,udp |
Network layer |
To select a route for a packet |
Ip,icmp,rip,ospf,bgp,igmp |
Data Link Layer |
Transmit address-enabled frames and error detection |
Slip,cslip,ppp,arp,rarp,mtu |
Physical Layer |
Transfer data on physical media in binary data form |
iso2110,ieee802. IEEE802.2 |
---reproduced from OSI Network Seven layer protocol and four layer protocol
So can PHP actually be a socket? The answer is YES! After all, is the most beautiful language in the world ! The theory based on Turing's completeness is true. There is a special socket implementation extension in PHP: Sockets. But people say that PHP can not and is not wrong. Because an important step in the socket implementation of PHP is to use the Socket_listen () function to implement port monitoring, this must be supported by the CLI. PHP is generally known to use CGI on the web side of the implementation, so based on CGI PHP is indeed not possible. But PHP is not without the CLI, hum ~
When you want HTTP and no curl extension.