HTTP, Socket and TCP Meng force

HTTP, Socket, TCP these three concepts are often referred to in the development of the network, but also relatively easy to be confused with the concept of the three in the TCP/IP agreement in the location of the relationship:

HTTP, Socket and TCP Meng force
three position relationship.Png

Among them, HTTP is an application layer protocol, a network protocol is the most commonly used development; TCP is a transport layer protocol, computer networks are aware of the University, there is a layer of UDP protocol; and Socket is from the transport layer abstraction, using IP address plus end form slogan logo, and not in the traditional sense of the network protocol, so the protocol, the three are easy to distinguish. So HTTP connection, TCP connection, Socket connection what is the difference?

1, TCP connection and HTTP connection

In network layer, HTTP protocol is based on the TCP protocol, the client to the server sends a HTTP request, need to establish a TCP connection with the server, also three handshake is the classic (which is usually difficult to detect the number of users), according to the interaction to shake hands after success. HTTP is based on request response model and stateless protocol, before 1.1 only supports short connection is a connection request response after the interruption, the next request needs to carry on the TCP connection, after 1.1 to support long connection after a TCP connection, the client can send multiple HTTP requests to the server.

Summary: HTTP based on TCP

2, TCP connection and Socket connection

Socket is the same abstract layer between the application layer and the transport layer, it is a set of interfaces, so the Socket connection can be based on the TCP connection, also may be based on UDP. We know that TCP is a reliable protocol, the UDP protocol is not reliable, so the TCP protocol based on Socket connection is also reliable; the UDP protocol based on Socket connection is not reliable, most instant messaging tools are based on the.

Summary: Socket can be based on TCP or UDP

3, HTTP connection and Socket connection

HTTP 1.1 is a short before the connection, the TCP protocol based on Socket connection is a long connection, although HTTP1.1 began to support the long connection, but unlike the Socket connection once established, unless one party voluntarily disconnected or connected state remains. Ttdevs added to me: TCP based Socket may be a short connection, it may be a long connection, long connection may need to maintain some means, such as heart beat, each has a different application scenarios. Rather than a simple “TCP protocol based on the Socket connection is a long connection”. After looking up the relevant information on the Internet, there is a sentence: “during the TCP connection to maintain, if there is no packet to send, send packets (heartbeat) regularly to maintain the connection state. “Because I have not done this specific work, if there is a great God can be detailed, welcome to correct me! )

HTTP connection, only after the client request the server will respond, the server is unable to send messages to the client. The Socket connection, the communication between the two sides to send messages and there is no limit, either party can send messages to the other side at any time.

4, HTTP Or Socket?

With HTTP: the two sides do not need to maintain the connection, the client is only through a HTTP request to obtain a specific server resources. Such as through the get/post request to get a web page, pictures, JSON or XML data, as well as the commonly used file upload, Download Small files, etc..

Socket: most instant messaging applications (known that part of QQ’s function is based on the TCP, because TCP always needs three handshake, although reliable but when the network is not good miserable), chat (UDP+ news broadcast based, large file transfer etc.).