To understand the HTTP and socket first to be familiar with the network layer seven: The number of the net transfer table should be, 1
1
HTTP protocol: Hypertext Transfer Protocol, which corresponds to the application layer, is used to encapsulate the data.
TCP/UDP protocol: Transmission protocol, which corresponds to the transport layer, mainly solves the data transmission in the network.
IP protocol: Corresponds to the network layer, also solves the data in the network transmission.
When transmitting data, only the TCP/IP protocol (Transport layer) is used, and if there is no application layer to identify the data content, the transmitted protocol is useless.
Application layer protocol Many ftp,http,telnet, etc., you can define the application layer protocol.
The web uses HTTP as the Transport Layer protocol to encapsulate HTTP text information and then use TCP/IP as the Transport layer protocol to send data to the network.
first, the HTTP protocol
HTTP is a short connection: the client sends a request that requires a server-side loopback response. After the request is over, the link is released actively, so it is a short connection. As a general rule, you do not need any data, and you want to keep a "keep-connected" request to the server at regular intervals. This ensures that the client is "on the Go" state on the server side.
The HTTP connection uses a "request-response" approach, not only to establish a connection on the request, but also to return the data to the server after the client requests it.
Second, Socket connection
To understand the Socket, you must understand the TCP connection.
TCP Three-time handshake: The handshake process does not transfer data, after the handshake, the server and the client start transmitting data, ideally, once the TCP connection is established, the TCP connection will persist until either side of the communication is actively disconnected.
Socket is the TCP/IP protocol encapsulation, socket is only an interface is not protocol, through the socket we can use the TCP/IP protocol, in addition to TCP, you can also use the UDP protocol to pass data.
When creating a socket connection, you can specify the transport layer protocol, which can be TCP or UDP, which is a TCP connection and vice versa.
Socket principle
Socket connection, requires at least a pair of sockets, divided into Clientsocket,serversocket connection is divided into 3 steps:
(1) Server monitoring: The server does not locate the specific client socket, but at all times in the listening state;
(2) Client request: The socket of the client to describe the socket of the server it is connecting to, provide the address and port number, and then make a connection request to the server socket;
(3) Connection confirmation: When the server socket receives a request from the client socket, it responds to the client socket request and establishes a new thread that sends a description of the server-side socket to the client. Once the client confirms this description, the connection is formally established. While the server socket continues to be listening, it continues to receive connection requests from other client sockets.
socket is a long connection: Normally the socket connection is a TCP connection, so once the socket connection is established, the communication parties begin to send data content to each other until the two sides are disconnected. In the actual application, because of the network node too much, in the transmission process, will be disconnected by the node, so to poll the high-speed network, the node is active state.
In many cases, it is necessary for the server to actively push the data to the client, to keep the client and the service side of the real-time synchronization.
If both are Socket connections, the server can send data directly to the client.
If the two sides are HTTP connections, the server needs to wait for the client to send the request before the data can be passed back to the client.
Therefore, the client periodically sends a request to the server, not only to remain online, but also to ask the server if there is new data, and if so, pass the data to the client.
Reference Original: Http://www.cnblogs.com/xclidongbo/p/4032904.html?utm_source=tuicool&utm_medium=referral
Transferred from: http://www.cnblogs.com/meier1205/p/5971313.html
The difference and connection of Tcp/ip,http,socket