The difference between TCP/IP, Http, and socket

Source: Internet
Author: User
Tags ack

1, to understand the HTTP and socket first to be familiar with the network layer seven: The number of net transfer table should be,

2,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.

Thirdly, the network is divided into the physical layer, the data link layer, the network layer, the transport layer, the conversation layer, the presentation layer and the application layer. Through the preliminary understanding, I know that the IP protocol corresponds to the network layer, the TCP protocol corresponds to the transport layer, and the HTTP protocol corresponds to the application layer, the three are not inherently comparable, the socket is the TCP/IP protocol encapsulation and application (programmer level). It can also be said that the TPC/IP protocol is the Transport Layer protocol, mainly to solve how the data in the network transmission, and HTTP is the application layer protocol, mainly to solve how to package the data. Regarding the relationship between TCP/IP and HTTP protocol, the network has a relatively easy to understand introduction:

"When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but in that case, if there is no application layer, it will not be able to identify the data content, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, such as HTTP, FTP, Telnet, etc., You can also define the application layer protocol yourself. The web uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol. ”

And we usually say the most socket is what, in fact, the socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol. In fact, the socket is not necessarily associated with the TCP/IP protocol. The socket programming interface is designed to adapt to other network protocols as well. So, the advent of sockets just makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as Create, listen, connect, accept, send, Read and write, and so on. The network has a section on the socket and TCP/IP protocol relationship is relatively easy to understand:

"TCP/IP is just a stack of protocols, just like operating systems, which must be implemented in a specific way, as well as providing an external interface for operations." This is like the operating system will provide a standard programming interface, such as the Win32 programming interface, TCP/IP should also provide the interface for programmers to do network development, this is the socket programming interface. ”

About the TCP/IP protocol only, with the broad and profound I think not too, just check the online about this kind of information and books only the number of documents to know, this I intend to buy some classic books (such as "TCP/IP Detailed: Volume One, Volume II, Volume three") to learn, Today, we summarize some of the knowledge based on TCP/IP protocol-based applications and programming interfaces, which is just a lot of HTTP and sockets.

CSDN has a comparative image of the description: HTTP is a sedan, providing a specific form of encapsulation or display data; the socket is the engine that provides the ability to communicate on the network.

In fact, the transport layer TCP is based on the network layer of IP protocol, and the application layer of the HTTP protocol is based on the transport layer of the TCP protocol, and the socket itself is not a protocol, as mentioned above, it just provides a TCP or UDP programming interface.

Here are some of the important concepts that are often encountered in a written test or interview and are summarized here.

Four, what is the three-time handshake for a TCP connection

First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;
Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;
Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.
The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection. When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a "four handshake" (the process is not fine-grained, that is, the server interacts with the client and finally determines the disconnection)

Five. Steps to establish a network connection using the socket

Establishing a socket connection requires at least one pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket.

The connection between sockets is divided into three steps: Server listening, client request, connection acknowledgement.

1. Server monitoring: Server-side sockets do not locate specific client sockets, but are waiting for the status of the connection, real-time monitoring network status, waiting for the client connection request.

2. Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

3. Connection confirmation: When a server-side socket hears or receives a connection request from a client socket, it responds to a client socket request, establishes a new thread, sends a description of the server-side socket to the client, and once the client confirms the description, the two sides formally establish the connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.

Six, the features of the HTTP link

The HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the most commonly used protocols for mobile networking, an application built on the TCP protocol.

The most notable feature of an HTTP connection is that each request sent by the client requires a server loopback response, and the connection is actively released after the request has ended. The process from establishing a connection to closing a connection is called a "one-time connection."

Seven, the difference between TCP and UDP

1. TCP is link-oriented, although the network's insecure instability determines how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at least (in fact, to a large extent guaranteed) to ensure the reliability of the connection, and UDP is not a connection-oriented, UDP transmission data before the connection with the other party, the data received is not sent to confirm the signal, the sender does not know whether the data will be properly received, of course, there is no need to resend, so that UDP is a non-connected, unreliable data transmission protocol.

2. Also due to the characteristics of 1, so that the cost of UDP more small data transmission rate is higher, because there is no need to send and receive data confirmation, so UDP real-time better.

Know the difference between TCP and UDP, it is not difficult to understand why the use of TCP transmission protocol MSN than UDP transmission file slow, but can not say that QQ communication is not safe, because the programmer can manually verify the data sent and received by UDP, For example, the sender of each packet number and then by the receiver to verify AH what, even so, UDP because the package on the underlying protocol does not adopt a TCP-like "three-time handshake" to achieve the TCP can not achieve the transmission efficiency.

The difference between TCP/IP, Http, and socket

Related Article

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.