HTTP of TCP / IP appetizer

SoWhat1412 2021-02-23 03:27:04
http tcp ip appetizer


1.1  TCP/IP Definition

TCP/IP A protocol family is a set of protocols , Also called Internet protocol family , The only way for computers to follow these rules , To communicate .TCP and IP It's just that 2 It's an important agreement , So use TCP/IP To name this internet protocol family , In fact, he basically includes four layers of agreement .

1.2 TCP/IP   function

As mentioned above TCP/IP Macroscopically, it is divided into four layers , Next, the specific functions of the next four layers .

1.2.1. application layer

application layer Provide different network service protocols directly for users , such as HTTP、Email、FTP etc. , These protocols are designed to meet different needs in real life . Users also operate and assemble data in this layer most of the time , To put it bluntly socket Programming ! As for the specific data is how to network transmission , It's up to the next three levels .

1.2.2. Transport layer

The transport layer provides communication services for the application layer , Belong to the highest level of communication oriented part , It's also the bottom layer of user functions . The transport layer provides logical communication for applications that communicate with each other . It mainly includes TCP The protocol and UDP agreement .

  1. TCP Provide connection oriented data flow support 、 reliability 、 flow control 、 Multiplexing and other services .

  2. UDP No complex control mechanism .

The role of the transport layer :

  1. Segment and encapsulate the data from the application layer .

  2. Provide end-to-end transport services .

  3. Build logical communication between sending host and receiving host .

1.2.3. The network layer

The function of network layer is to realize data packet Route selection and forward . Wan usually uses many hierarchical routers to connect to distributed hosts or LANs , therefore , The two communication hosts are generally connected through multiple intermediate node routers . The task of the network layer is to select these intermediate nodes , To determine the communication path between the two hosts . At the same time, the details of network topology connection are hidden from the upper layer protocol , So that in the transport layer and network applications , The two sides of the communication are directly connected .

IP The agreement is at this level , Provides routing and addressing functions , Enable two terminal systems to interconnect and determine the best path , And has the certain congestion control and the flow control ability .

1.2.4. The link layer

The data link layer implements the network driver of the network card interface , To handle the transmission of data over physical media . Two commonly used protocols in the data link layer are ARP agreement (Address Resolve Protocol, Address resolution protocol ) and RARP agreement (ReverseAddress Resolve Protocol, Reverse Address Resolution Protocol ). They achieve IP Address and machine physics MAC The translation between addresses .

1.2.5 The data transfer

  1. utilize TCP/IP When protocol family communicates with network , Communicate with each other in hierarchical order . The sender goes down from the application layer , The receiver goes up from the link layer .

  2. When the sender transmits data between layers , When passing through a layer, it will be marked with the first information of the layer . conversely , When the receiver transmits data between layers , When passing through a layer, the corresponding head will be eliminated .

  3. This way of wrapping data and information is called encapsulation .

But one thing to note , IP There are layers Maximum Transmission Unit Maximum transmission unit MTU The limit , In the same way, in a data transmission TCP There are layers Maximum Segment Size Maximum segment length MSS The limit ,

Ethernet MTU yes 1500, basic IP The length of bow is 20,TCP The first is 20, therefore MSS The maximum value of is 1460(MSS Excluding the first part of the agreement , Only application data ).

So a large application layer information transmission may be divided into several blocks and then transmitted one by one . The receiver receives the application layer data of each packet and assembles it into application layer data , Then a request is received , This is also Content-Length The meaning of field existence .

Data packet transmission

1.3 OSI Follow TCP/IP


  • OSI also called   Open system interconnection communication reference model , It's a conceptual model proposed by the international organization for standardization , A standard framework that attempts to interconnect computers worldwide as networks , It focuses on what the necessary functions of the communication protocol are .


  • The real network transmission protocol in real life , Focus on the implementation of the protocol on the computer, which program should be developed .

OSI Follow TCP/IP difference

  1. OSI Introduced services 、 Interface 、 agreement 、 Concept of layering ,TCP/IP Learn from it OSI These concepts are based on TCP/IP Model .

  2. OSI There's a model before a protocol , There are standards before practice .

  3. TCP/IP First there are protocols and applications, then there are models , And it's a reference OSI Model .

  4. OSI It's a theoretical model , and TCP/IP Has been widely used , Become the de facto standard of Internet connection .

After the introduction of the macro TCP/IP After protocol cluster , Now let's go From top to bottom Enter the world of the Internet .


2 application layer HTTP

2.1  HTTP Simple understanding

2.1.1 HTTP Definition

HyperText Transfer Protocol, also called Hypertext transfer protocol .HTTP It's about transferring text between any two points in the computer world 、 picture 、 Conventions and specifications for hypertext data such as audio and video .

2.1.2 URI、 URN 、URL

URI:Uniform Resource Identifier Uniform resource identifier , It means web Every available resource on the ,URI It's just a Concept , It doesn't matter how , The point is to identify a resource .

URN :Universal Resource Name Unified resource name , By a unique name in a specific namespace or ID To identify resources .

URL:Universal Resource Locator Uniform resource locator ,URL It's actually URI A subset of , It not only identifies a resource, but also tells you how to access it , A standard URL Must include :protocol、host、port、path.

URL Templates
  1. protocol: What kind of protocol is adopted by both sides of communication ,HTTP、ftp、file etc.

  2. IP: The reality of the server IP Address .

  3. Port: Service resources in IP Exposed ports on the machine .

  4. path: The storage path of resources on the server , It's usually a file or access directory .

  5. query: Optional configuration , use & Division , Parameter with KV Way to store .

Give an example of the relationship between the three

  1. You want to find someone , People here are a resource URI.

  2. If you use ID number + I'll find the name URN, ID number + The name only identifies people as a resource , But we can't confirm the address of the resource .

  3. If you use the address :XX province XX City XX District XX unit XX The occupants of the room Namely URL, It's not just about people , And located its address

2.2 HTTP Message format

request and Respond to All messages are sent by Start line Head Blank line Entity Four parts , It's just Start line not quite the same .

2.2.1 request

Request message format   Request line

The request line contains 3 Parts of : Request method 、URL、 Protocol version . They are separated by spaces , The request line ends with a carriage return + A newline ending .

Request method : Indicates what you want to do with the target resource ,HTTP1.1 Defines the 8 Request method , The most common one is GET and POST.

URL: The specified address is the target address of this visit .

Protocol version : Specifies the... Currently supported by the client HTTP edition ,HTTP At present, the common ones are 1.1、 2.0、3.0 Three versions , If the requestor specifies 1.1, The responder will also use HTTP 1.1 Reply to the agreement . Request header

Request header To inform the server of the request and some additional information about the client itself , Each request header is a Key value pair , The key and value are separated by a colon . Each request header forms a separate line , They all end with a carriage return and a newline . In all request headers , Only Host It's necessary , Other request headers are optional , List some common request headers : Blank line

Contains only a carriage return and a line feed , Nothing else . This blank line marks the end of the request header , It is necessary . Request body

It's usually user-defined Information body , In the header, you can pass Content-Type Specify the type . Request sample
Request sample

2.2.2 Respond to

Response message format   Response line

Specifies the return information corresponding to HTTP edition 、 Response information status code 、 Simple reason .   Response head

As for blank lines and message bodies, they are almost like requests , The message body type is determined by the Content-Type designated . Sample response
Sample response

2.3 HTTP Header fields

HTTP The protocol specifies a lot of header fields , Can achieve a variety of functions , But basically it can be divided into the following four categories :

  1. General fields : It can appear in both the request header and the response header .

  2. Request fields : Can only appear in the request header , Further explain the request information or additional conditions .

  3. Response fields : It can only appear in the response header , Supplement the information of the response message .

  4. Entity fields : It's actually a generic field , But specifically describe body Additional information about .

Through to HTTP Setting of header field ,HTTP It provides the following important functions :

  1. Content negotiation : The client and the server have agreed on the content of the response resource , Like language 、 Character set 、 Encoding mode 、 Compression type .

  2. Cache management : According to the characteristics of resources, we can check whether the resources are cached to the client , Be careful max-age、no-cache、no-store、must-revalidate The difference between .

  3. Entity type : Through analysis Content-Type To get requests and responses MIME type .

  4. Connection management : Read configuration parameters to achieve long and short connection .

2.4 HTTPS Follow HTTP

HTTP It's transmitted in clear text , There are several risks :

  1. The risk of eavesdropping : Confidentiality of information , For example, the communication content can be obtained on the communication link .

  2. Risk of tampering : Information integrity , For example, forcing spam .

  3. Pretend to be a risk : Identity recognition , For example, fake websites like Taobao .

2.4.1 SSL/TLS summary

To ensure safety HTTPS emerge as the times require ,HTTPS stay HTTP And TCP Between the layers added SSL/TLS Cryptographic protocol , These three problems can be solved .
  1. adopt Mixed encryption Achieve the confidentiality of information .

  2. adopt Abstract algorithm The way to achieve integrity , It can generate unique serial numbers for data .

  3. Put the server Public key Put in digital certificate in , It's solved Pretend to be The risk of .

Here we need to pay attention to the general situation HTTP The default is 80 port , and HTTPS Default 443 port .

2.4.2 encryption algorithm

encryption algorithm It is divided into Symmetric encryption Follow Asymmetric encryption .

  1. Symmetric encryption : Encryption and decryption use a key , It's fast , The key must be kept secret , No secure key exchange . Common encryption algorithms are AES、DES、RC4、BlowFish etc. .

  2. Asymmetric encryption : Use Public key and Private key Two keys , The public key can be distributed arbitrarily while the private key is kept secret , Solved the key exchange problem, but it was slow . The derivation from private key to public key is one-way , It can guarantee the security of the private key . Common encryption algorithms are RSA、 DSA、Diffie-Hellman etc. .

HTTPS It's using Symmetric encryption  +   Asymmetric encryption  = Mixed encryption The way :

  1. Before the establishment of communication Asymmetric encryption Exchange secret keys in different ways , In the future, asymmetric encryption will not be used .

  2. In the process of communication Symmetric encryption Encryption of plaintext data by means of session secret key .

2.4.3 Abstract algorithm

The main feature of the algorithm is that the encryption process does not need a key , And encrypted data cannot be decrypted , At present, the only thing that can be decrypted is CRC32 Algorithm , Only by inputting the same plaintext data and passing through the same message digest algorithm can we get the same ciphertext .

Message digest algorithm is mainly used in digital signature field , As a summary algorithm for plaintext . The famous summarization algorithms are RSA The company's MD5 Algorithm and SHA-1 Algorithms and a lot of variations .

Verify integrity
  1. The client generates the digest from the plaintext data through the specified digest algorithm .

  2. Plaintext data + Abstract algorithm It's encrypted by public key and then transmitted .

  3. After receiving the message, the server decrypts the message with the private key to get the plaintext + Abstract .

  4. The server uses the same digest algorithm to digest plaintext .

  5. Compare the two summaries generated by the client and the server , In order to check whether the data is complete .

2.4.4 CA certificate

Asymmetric encryption , The client saves the public key , How to ensure the accuracy of public key is a difficult problem , If someone steals the server's public key to do something , In the whole process of data transmission, the client and server are not aware of the existence of a third party , But the information has been leaked for a long time !

Asymmetric encrypted information disclosure

The key to the problem is How to ensure that the client receives the server's public key ! here digital certificate And that's what happened , It encrypts data based on the private key mentioned above , Public key decryption to verify its identity .

CA Ensure that the public key is transmitted correctly
  1. CA yes authority Certification authority , There are only a few authoritative companies in the world , The organization uses RSA Generate a pair of public and private keys .

  2. Server public key content + Issuer ID + To whom is the certificate issued Subject + The period of validity + Other information =   In plain text P

  3. In plain text P after Hash Algorithm to generate H1, use CA The private key pair of H1 Conduct RSA Encrypted access S.

  4. P + S  = digital certificate .

  5. After the client gets the digital certificate , Use the same Hash Algorithm to P Conduct Hash To calculate the H2.

  6. We use it CA Public key decryption S I got a H3.

  7. Compare H2 Follow H3 Whether or not the same , It also shows that this certificate OK. Different explanation P Has been modified or the certificate is not CA Issued by .

  8. You can take out the server public key correctly , Get it done !

2.4.5 SSL/TLS Establish process

to TCP Three handshakes of , Then prepare for encrypted communication , Before you start encrypting communications , Client and server must first establish connection and exchange parameters , This process is called handshaking HandShake, That's what I said all the time SSL/TLS modular , So what is its main workflow , You can think of it as ClientHello、ServerHello、Finish.

SSL/TLS Establish process
  1. Client request

The client sends an encrypted communication request to the server : Given by client SSL/TLS Agreement version No + A client generated random number Random1 + Client supported encryption methods .

  1. Server request

Server side confirmation SSL/TLS Does the version support , Confirm the encryption algorithm used , Generate random number Random2 ( Used to generate session secret key ), Build server digital certificate .

  1. Client certificate verification

  1. Client pass CA The public key confirms the authenticity of the server's digital certificate , Retrieve the server public key .

  2. The client generates a random number Random3, Encrypted with the server's public key PreMaster Key And send it to The server , Send another appointment encryption algorithm .

  3. The server decrypts with the private key PreMaster Key obtain Random3. So far, both the server and the client use the same encryption algorithm Random1 + Random2 + Random3 = Conversation key Session Key, Later communication will use this to encrypt communication .

  4. The client generates a summary of the previous handshake message and encrypts it with the agreed secret key , This is the first encrypted message sent by the client . The server will decrypt with the secret key after receiving it , If it can be solved, it means that the secret keys negotiated above are consistent .

  1. The server finally responded

  1. Server received Random3 + Final encryption algorithm It is finally decided that   Conversation key Session Key.

  2. The server tells the client that the encryption algorithm has changed , I'll use it later Session Key Encrypted information .

  3. The server will also generate a digest of the handshake message and encrypt it with a secret key , This is the first encrypted message sent by the server . The client will decrypt with the secret key after receiving it , It shows that the key to negotiation is the same .

  1. Send data normally

  • thus , Both sides have safely negotiated the same secret key , SSL/TLS The handshake phase is over . All application layer data will be encrypted with this secret key before passing through TCP For reliable transmission .

2.4 HTTP The history of

at present HTTP Version is divided into HTTP/1.1、HTTP/2、HTTP/3 Three versions , The mainstream uses the first two .

HTTP Version comparison
2.4.1  HTTP/1.1

HTTP/1.1 Compared with the old version, the advantages and disadvantages are as follows :


  1. TCP Start using long connections instead of short ones to avoid unnecessary performance overhead .

  2. Like sending ABC when B There's no need to wait for the delivery of A Start sending after sending B.


  1. request / The response header is sent without compression , Can only compress Body part .

  2. Send redundant configuration information back and forth .

  3. Can cause head obstruction .

  4. FIFO Pattern , There is no concept of priority .

  5. Only client requests , Server response .

2.4.1  HTTP/2

HTTP/2 The protocol is based on HTTPS Of , We have done the downward compatibility and the following optimization .

  1. The head of compression : introduce HPACK Algorithm , Maintain a header table at both the client and the server , All the fields are stored in this table , The head repeats the information back and forth, no longer sends the original value, just send the index number directly .

  2. Binary transmission : The new version uses a more computer friendly binary mode of transmission , Data is transmitted by frame .

  3. Streaming priority transmission : Press Stream Distinguish between different request response packets , Every Stream They all have separate numbers . And you can also specify priorities .

  4. Multiplexing : Multiple streams in a connection can send and receive requests at the same time - Reply data frame , The packets in each stream are transmitted and assembled in sequence , Each stream is independent , So who's going to deal with the request first , Who can send the response to the other party first through the connection .

  5. Server push : The server will take the initiative Push may be used JS、CSS etc. static Variable .


  1. Blocking problem :HTTP/2 The frame transmission is carried out in the application layer , The final data has to be processed TCP transmission , and TCP It's a reliable connection , With packet loss retransmission function . If there is a packet loss, it will result in all HTTP The request is waiting for the lost packet to be retransmitted .

2.4.1  HTTP/3

HTTP/3 hold TCP The agreement was changed to UDP, because UDP It doesn't matter the order 、 No matter what the packet loss is , meanwhile Google stay UDP It's also added on the basis of TCP Connection management 、 Congestion window 、 Flow control and other mechanisms , This set of agreements we call QUIC agreement . As a whole HTTP/3 The optimization points are as follows :

  1. QUIC A unique mechanism to ensure the reliability of transmission . When a packet loss occurs to a stream , It will only block the flow , Other streams will not be affected .

  2. TLS The algorithm is also developed by 1.2 Upgrade to 1.3, The head compression algorithm is upgraded to QPack.

  3. HTTP/3 Three communications before that TCP handshake + TLS Three encryption interactions .QUIC The bottom layer will 6 The two steps merged into one 3 Step .

  4. QUIC It's a UDP Above TCP + TLS + HTTP/2 The protocol of multiplexing .

2.5  HTTP characteristic

  1. Flexible expansion

HTTP What's so powerful is that he just prescribed header +  body Basic framework , It can be customized , At the same time, its bottom layer is pluggable components , such as SSL/TLS The addition of , Binary frame transfer ,UDP Replace TCP wait .

  1. Reliable transmission

Whether it's TCP still QUIC It's guaranteed that Reliability of data transmission .

  1. request - Response mode

HTTP yes be based on - request The response model realizes the transmission of data .

  1. No state

HTTP Every request of - Responses are stateless , Therefore, each sending and receiving message is completely independent , If you want to achieve some chain reaction, you need to use Session Follow Cookie Mechanism .

  1. Application layer protocol

HTTP It's just a transport protocol defined in the application layer , The bottom layer of it is TCP Protocol transfer data .

2.6  HTTP Common status code

common HTTP Status code There are five types .

3 appendix

I only gave a general explanation TCP/IP The application layer and the transport layer of the protocol , Network layer , Look at a more detailed version TCP/IP agreement .

TCP/IP agreement

4 Reference resources

  1. SSL/TLS:

  2. HTTP Ten thousand character handout :

  3. Special topic of Kobayashi network :

  4. HTTP Status code :

  5. TCP/IP Explain :

Six questions Kafka Why are you so good !


20 This picture takes you to HBase Travel around the world


interviewer : About Spring That's the question 13 individual


JAVA Twelve moves in a row , Can you catch it ?


Redis: From application to bottom , Yiwen will help you



  1. An inexperienced front-end engineer, what are the common problems when writing CSS?
  2. HttpServletRequest get URL (parameter, path, port number, protocol, etc.) details
  3. Springboot starts http2
  4. Enabling http2.0 in spring boot
  5. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片
  6. Using JavaScript in Safari browser history.back () the page will not refresh after returning to the previous page
  7. vue.js Error in win10 NPM install
  8. In less than two months, musk made more than $1 billion, more than Tesla's annual profit
  9. Springboot starts http2
  10. Vue event bus
  11. JQuery easy UI tutorial: custom data grid Pagination
  12. Using okhttp and okhttpgo to obtain onenet cloud platform data
  13. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control
  14. HTTP 1. X learning notes: an authoritative guide to Web Performance
  15. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control
  16. HTTP 1. X learning notes: an authoritative guide to Web Performance
  17. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片
  18. Event bubble and capture in JavaScript
  19. The root element is missing solution
  20. Event bubble and capture in JavaScript
  21. Configure the certificate to enable ngnix to publish the trusted website of HTTPS
  22. Javascript数据类型
  23. HTTP interface debugging tool! 48000 star HTTP command line client!
  24. Parameter encryption of front end URL link band
  25. HTTP interface debugging tool! 48000 star HTTP command line client!
  26. Three front end frameworks: data binding and data flow
  27. Reading Axios source code (1) -- exploring the realization of basic ability
  28. Event bubble and capture in JavaScript
  29. 【微前端】微前端最終章-qiankun指南以及微前端整體探索
  30. R & D solution e-Car front end monitoring system
  31. [JS] 877 - 35 wonderful knowledge of JavaScript, long experience!
  32. R & D solution e-Car front end monitoring system
  33. High performance nginx HTTPS tuning - how to speed up HTTPS by 30%
  34. 解决ajax跨域问题【5种解决方案】
  35. Top ten classic sorting of JavaScript
  36. HTTP 1. X learning notes: an authoritative guide to Web Performance
  37. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control component
  38. My http / 1.1 is so slow!
  39. Why Vue uses asynchronous rendering
  40. The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for
  41. The tapable instance object hook of webpack4. X core tool library
  42. The tapable instance object hook of webpack4. X core tool library
  43. Using libcurl for HTTP communication in C + +
  44. Using libcurl for HTTP communication in C + +
  45. Using CSS variable in Vue
  46. Deeply understand the update of state and props in react
  47. No matter how fast the Internet is, it's useless! In addition to Baidu disk, there is this website slow to let you eat shriveled
  48. Baidu share does not support the solution of HTTPS
  49. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  50. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  51. Vue cli creates vue3 project
  52. Nginx reverse proxy for windows authentication using NTLM
  53. Rust tutorial: introduction to rust for JavaScript developers
  54. Deploying personal blog to Tencent cloud with serverless framework
  55. R & D solution e-Car front end monitoring system
  56. JavaScript advanced learning
  57. Spend 1 minute to master these 5 ppt tips, courseware making less detours
  58. Vue: vuex persistent state
  59. React native gets the current network state of the device Netinfo
  60. High performance nginx HTTPS tuning - how to speed up HTTPS by 30%