HTTP, TCP, UDP, Socket interpretation

Source: Internet
Author: User
Tags ack
This is a creation in Article, where the information may have evolved or changed.

1. Guidance

Remember to go to college in the "Computer Network Foundation" specialized class, at that time is really pure kind ignorance, think I later do not engage in network this piece of things, do not need to study computer network This course, as long as the exam pass on the line. But after work I found that whether we do software development, will be more or less contact with the computer network, because the computer network everywhere. Ordinary life we want to touch the router, switch, dns,ping, modify DNS and so on these basic operations, as a software developer, we want to contact TCP, UDP, HTTP, TCP connection three times handshake, disconnect four wave, socket and so on these professional points of knowledge, This is really the knowledge of these professional points is really a book to the time Square hate Ah, next we will review it well.

The OSI is an abbreviation for open System interconnection, which means that it is connected to the opening systems. The International Organization for Standardization (ISO) has developed the OSI model, which defines the standards for the interconnection of different computers and is the basic framework for designing and describing computer network communication. The OSI model divides the work of network communication into 7 layers, namely physical layer , Data link layer , network layer , Transport layer , conversation layer , presentation layer and application layer. .

Memory method: Remember the first word in front

The list of the number of objects in the network should be

Each layer of these seven layers has its own role, such as decomposition:


Write a picture description here

2. Http

HTTP (hypertext Transfer Protocol, Hypertext Transfer Protocol) is the protocol for the application layer in the OSI seven layer.

Protocol Explanation:

HTTP is an application-layer protocol based on the TCP/IP protocol. It does not involve packet (packet) transmission, mainly specifies the communication format between the client and the server, using 80 ports by default.

Some people say that HTTP is a TCP/IP communication protocol to pass data (HTML files, image files, query results, etc.), I think this sentence has a problem, if HTTP is defined as a protocol then HTTP is just a rule that specifies the URI request format of the client and the service-side data response format, To say that the HTTP request this concept for a long time more general, the client sends an HTTP request, through the OSI Layer Seven Protocol assembly, transmission, splitting, transmission and other operations, the final can obtain the data we want.

Description of Use:

Resolve how the data on the client and server is packaged, and the final data on both ends of the package can be identified.

Relationship Description:

The TPC/IP protocol is the Transport Layer protocol, which mainly solves how data is transmitted in the network, and HTTP is the application layer protocol, which mainly solves how to wrap the data, while socket is the encapsulation and application of TCP/IP protocol (programmer level). The HTTP protocol is actually built on top of the TCP/IP protocol. , almost every language has its own HTTP implementation, of course, also has its own socket implementation, such as Golang, Java, in their own net package can find the source code. Using the TCP protocol is the need to connect three handshake, disconnect three times to wave.

HTTP is a stateless protocol that does not manage the state of requests and responses that have occurred before. In other words, this request cannot be processed according to the previous state. However, using cookies and sessions can save status and recognition. HTTP can only be initiated by the client, the server can not actively want to send data to the client.

2. TCP

TCP (transmission Control Protocol, Transmission Protocol) is the Protocol for the Transport layer in the OSI layer seven.

Protocol Explanation:

TCP (transmission Control Protocol, transmission Protocol) is a connection-based protocol, which means that a reliable connection must be established with each other before the data is formally sent and received. TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively shuts down the connection. When disconnected, both the server and the client can proactively initiate a request to disconnect a TCP connection.

Where does safety manifest itself?
connection Security : three times handshake four times, please refer to: TCP detailed.
Data Security : TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other.

Description of Use:

Resolve the data in the network can be safely and without error transmission

Relationship Description:

HTTP protocol is TCP-based, because the HTTP request is a one-time response, because the request and response are only one time, so it is necessary to ensure the security of data, in this one transmission is not bad data loss, so HTTP is based on a more secure TCP protocol than the UDP protocol, The socket connection can be based on TCP or UDP, depending on the needs of the specific business. the HTTP request uses a TCP-based socket connection .

TCP sends the packet has the serial number, the other party receives the packet to give a feedback, if has not received the feedback to automatically perform the time-out resend, therefore the TCP biggest advantage is reliable. General Web page (HTTP), Mail (SMTP), remote connection (Telnet), file (FTP) transfer is used with TCP.



TCP Interaction Flowchart

3. UDP

UDP (user Datagram Protocol, Subscriber Datagram Protocol) is a connectionless transport layer protocol in the OSI Reference Model.

Protocol Explanation:

UDP User Datagram Protocol is a simple non-connected Transport layer protocol for datagram. UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is very fast.

Description of Use:

Solve the data in the network can be transmitted efficiently

Relationship Description:

In the OSI reference Model, only TCP and UDP two, TCP need link security efficiency is low, UDP wireless connection is not safe and efficient, we usually use long HTTP protocol based on TCP, many languages have HTTP request encapsulation, it is very convenient, but want to use UDP is not so convenient , want to use UDP we need to write a socket UDP, socket UDP does not need to link, the client knows the server IP and port number to send data directly, socket TCP due to need to link so when using the heartbeat mechanism to ensure that the link is not broken.

UDP is commonly used for multipoint communication and real-time data services such as voice broadcast, video, QQ, TFTP (Simple File transfer), SNMP (Simple Network Management Protocol), RTP (real-time delivery protocol) RIP (Routing Information Protocol, such as reporting stock market, aviation information), DNS (Domain name interpretation). Pay attention to the smooth speed.


UDP Interactive Flowchart

4. Socket

Noun Explanation:

Socket is a TCP or UDP protocol encapsulation and implementation, socket is not a protocol, almost every language has implemented the TCP and UDP encapsulation and implementation of the socket code base, easy to develop programmers to use.

Description of Use:

Socket is the TCP or UDP protocol encapsulation and implementation, so that I can be based on the socket of your language to achieve more functions, there is a socket TCP-based HTTP request, there is a TCP long connection based on the socket message push, can also be based on the socket TCP /UDP Customize a set of your own communication protocols.

Relationship Description:

HTTP based on the tcp,http request needs to be done using socket TCP.

5, an HTTP request of the mental journey

Since an HTTP request will involve HTTP TCP Socket DNS and so on, figuring out the HTTP process will probably tell you how the network request works. So what exactly do we go through when we send an HTTP request?
A few steps to go through in an HTTP request and response:

(1) Domain name resolution

Domain name resolution Mind is the client's work, the client only server domain name (similar to www.baidu.com), do not know the IP address of the service side, the client needs to take the domain name to the DNS servers to obtain the corresponding IP address.

(2) Establishing a TCP connection

Initiated by the client, a TCP connection is established between the client and the server after a three-time handshake. The so-called three-time handshake (three-way handshake) means that when a TCP connection is established, the client and server are required to send a total of 3 packets.
The purpose of the three-time handshake is to connect the server to the specified port, establish a TCP connection, and synchronize the serial number and confirmation number of both parties and Exchange TCP window size information. In Socket programming, the client executes connect (). Will trigger a three-time handshake.

  
TCP Three-time handshake
First of all to understand a few signs, SYN (synchronous), Sync flag, ACK (acknowledgement), that is, the confirmation flag, SEQ should be sequence number, the meaning of the serial numbers, and the other four times the fin handshake, should be final, Represents the end flag.

First handshake: The client sends a TCP SYN flag where 1 of the packet indicates the port of the server to which the client intends to connect, and the initial sequence number x, which is stored in the header sequence number (Sequence) field.

Second handshake: The server sends back a confirmation packet (ACK) reply. That is, the SYN flag bit and the ACK flag bit are both 1, and the confirmation sequence number (acknowledgement number) is set to the customer's serial numbers plus 1, which is x+1.

Third handshake: The client sends a confirmation packet again (ACK) to the SYN flag bit for the 0,ACK flag bit of 1. and send the server an ACK to the ordinal field +1, put in the OK field sent to the other side. and write the serial number +1 in the data segment.

(3) initiating an HTTP request to send a request message

Initiated by the client, the client assembles the request message, sends the HTTP request to the server, HTTP by the fixed format, specifically wants to see:



HTTP request message

The HTTP request message consists of 3 parts (Request line + request header + request body), these three parts also have their own assembly format, which is not explained in detail here.

(4) server-side response HTTP request

The server receives the client's HTTP request after receiving the request message and responds to the request, which is to send the data to the client after processing the request message. The data sent to the client by the server is called the response message, assembled by the server, in the following format:



HTTP response Messages

① message protocol and version;
② Status code and status description;
The ③ response message header is also composed of several attributes;
④ response to the style, that is, we really want "dry".

(5) The client receives the response data message

In response to the style of the newspaper is what our customers really want, for Android/ios Mobile is generally JSON data, for the Web browser is generally HTML, the client receives response message after processing data.

(6) Close the TCP connection

In general, the TCP connection is due to the client actively shut down, the shutdown requires the client to actively initiate four waves, to shut down the TCP connection. In general, once the request and response are completed, the TCP connection is closed, and if the client or server joins this line of code in its header information:connection:keep-alive, the TCP connection will remain open after it is sent, The client can continue to send requests through the same connection. Maintaining a connection saves the time it takes to establish a new connection for each request and also saves network bandwidth.

The removal of TCP connections requires sending four packets, so called four waves (Four-way handshake). The client or server can initiate a wave gesture,

In programming, either side performs a close () operation to generate a waving action.


TCP loses four times to wave

In fact, there is a problem, why the connection is three times the handshake, when the closure is four times waving?

The syn+ack message can be sent directly after the server receives the SYN connection request message from the client side. Where the ACK message is used to answer, the SYN message is used for synchronization. However, when the connection is closed, when the server side receives the fin message, it is likely that the socket will not be closed immediately, so you can only reply to an ACK message, tell the client side, "You send fin message I received." I can't send fin messages until all the messages on my server end are sent, so I can't send them together. Therefore, four-step handshake is required.
   
 For example, the client actively sends a TCP shutdown request step:

First wave: The client sends a FIN, which indicates that the client needs to turn off the data transfer to the server and the client enters the fin_wait_1 state
 
Second wave: After receiving fin, the server sends an ACK to the client, confirming that the serial number is received +1,server enter close_wait status

Third Wave: server sends a fin to shut down server-to-client data transfer, Server enters Last_ack state

Fourth wave: After the client receives fin, the client enters the TIME_WAIT state, then sends an ACK to the server, confirming that the serial number is received by the serial number +1,server enter closed state, four times to complete the wave

Last diagram of the journey of an HTTP request:


Plot the mental journey of an HTTP request

Reference article:
HTTP, TCP, UDP, Socket knowledge Summary
Interview Review--android engineer's Network Foundation
Android Network request Journey

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.