当前位置:网站首页>Computer network: sorting out common network interview questions (I)

Computer network: sorting out common network interview questions (I)

2022-07-06 19:07:00 VoldemortQian


Preface

This article aims at sorting out the common interview questions of Jiwang

One 、 subject && answer

TCP and UDP The difference between ?

TCP

  • Provide connection oriented services . Connections must be established before data can be transferred , Release connection after data transfer .
  • TCP No broadcast or multicast service
  • because TCP To provide reliable , Connection oriented transport services , It's hard to avoid a lot of overhead , If you confirm , flow control , Timer and connection management . This not only makes the head of the protocol data unit much larger , It takes up a lot of processor resources .
  • TCP Generally used for file transfer 、 Send and receive mail 、 Remote login and other scenarios .
  • TCP Transfer in the form of byte stream , Unlike UDP Just like that, messages are transmitted independently , It is transmitted in byte stream mode without retaining the message boundary .
  • TCP Allow applications on both sides of the communication to send data at any time , because TCP There are buffers at both ends of the connection , Used to temporarily store data for two-way communication . Of course ,TCP You can send a segment of data immediately , You can also cache a period of time to send more data segments at a time ( The maximum segment size depends on MSS)

UDP

  • You don't need to establish a connection before transferring data , Remote host receiving UDP After the message , No confirmation required .
  • although UDP No reliable delivery , But in some cases UDP Is the most effective way to work ( Generally used for instant communication ), such as : QQ voice 、 QQ video 、 Live and so on .

The interviewer asked : Since you said TCP Reliable service provided , that TCP How does the protocol guarantee reliable transmission ?
There are mainly checksums 、 Serial number 、 Over time retransmission 、 Flow control and congestion avoidance :
1. Application data is divided into TCP Data block considered most suitable for sending .TCP Number every packet sent , The receiver sorts the packets , Send the ordered data to the application layer .
2. The checksum :TCP Will keep its head and data checked and . This is an end-to-end inspection and , The purpose is to detect any changes in data during transmission . If there is an error in the inspection and verification of the received section ,TCP The message segment will be discarded and the message segment will not be acknowledged .
3.TCP The receiver will discard the duplicate data .
4. flow control : TCP Each side of the connection has a fixed size buffer space ,TCP The receiving end of is only allowed to send the data that can be accepted by the buffer of the receiving end . When the receiver is too late to process the sender's data , It can prompt the sender to reduce the sending rate , To prevent the loss of bags .TCP Use sliding window to realize flow control .
5. Congestion control : When the network is congested , Reduce the sending of data .
6.ARQ agreement : It's also for reliable transmission , Its basic principle is to stop sending every packet , Waiting for confirmation . Send next group after receiving confirmation .
7. Over time retransmission : When TCP After sending out a paragraph , It starts a timer , Wait for the destination to acknowledge receiving the message segment . If you can't get a confirmation in time , This segment will be retransmitted .

 Insert picture description here

summary

  • TCP Provide reliable connection oriented services to the upper layer ,UDP Provide connectionless and unreliable services to the upper layer .
  • although UDP did not TCP The transmission is accurate , But it can also make a difference in many places with high real-time requirements
  • High requirements for data accuracy , The speed can be relatively slow , May choose TCP

Introduce to you TCP Three handshakes of

 Insert picture description here

TCP The three handshakes are divided into the following simple three steps :

  • First, the client sends a message with SYN Mark the packet to the server
  • Then the server sends a message with SYN/ACK Flag the packet to the client
  • Finally, the client sends a message with ACK Mark the packet to the server

The interviewer interrupted and asked : Why three handshakes ? once , Not twice? ?

The purpose of triple handshake is to establish a reliable communication channel , Simply speaking, it is the sending and receiving of data , The main purpose of the three handshakes is to confirm that the sending and receiving are normal , It can be considered from the following two aspects ( Let's assume that the client initiates the connection request first ):

1. Suppose that TCP It only takes two handshakes to connect , So if you shake hands the second time , The confirmation message returned by the server to the client is lost , The client thinks that the server has not established a connection with him , But the server thinks it has established a connection with the client , And maybe the server has started sending data to the client , But the client doesn't receive the data , Wasted resources . If it's three handshakes , There will not be a situation in which both parties start to send data before the connection is fully established .
2. If the server receives a connection request message from the client that has already failed , Will send a confirmation message to the client to agree to establish TCP Connect . But because the client does not need to send data to the server , So this time TCP And there's no point in connecting resources .

Therefore, three handshakes can confirm that the function of both transmitter and receiver is normal , Be short of one cannot

The interviewer asked again : The first 2 The handshake came back ACK, Why send back SYN?
The receiver returns the message sent by the sender ACK To tell the client , The message I've received is really the signal you sent , This shows that communication from the client to the server is normal . And back then SYN That is, the synchronization sequence number is used to establish and confirm the communication from the server to the client

Four waves are as follows
 Insert picture description here
Disconnect one TCP Connection requires the following four processes :

  • First wave : After the data sent by the client to the server is completed , Send the release connection message to the server , The message contains flag bits FIN=1, Serial number seq=u. At this time, the client can only receive data , Can't send data to the server .
  • Second wave : After the server receives the release connection message from the client , Send an acknowledgement message to the client , Contains the flag bit ACK=1, Serial number seq=v, Confirmation no. ack=u+1. At this point, the connection between the client and the server has been released , The client cannot send data to the server , The server can't send data to the client . But the one-way connection from the server to the client can also transfer data normally .
  • Third wave : After sending the data, the server sends the connection release message to the client , The message contains flag bits FIN=1, Sign a ACK=1, Serial number seq=w, Confirmation no. ack=u+1.
  • Fourth wave : The client receives a connection release request sent by the server , Send an acknowledgement message to the server , Contains the flag bit ACK=1, Serial number seq=u+1, Confirmation no. ack=w+1.

Interviewer asked : Why? TCP When you connect, it's 3 Time , When it was closed, it was 4 Time ?

because TCP The connection is aimed at the situation that the data transmission mechanism has not been established between the server and the client , and TCP There may be data transmission between the server and the client when closing
When the connection is closed , The client sends... To the server FIN when , It just means that the client no longer sends data, but can receive data . The server received... From the client FIN When the message , Let's go back to one ACK Reply message , The server may also have data to process and send , When the server no longer sends data , To send FIN
Now it means that the client agrees to close the connection . From the above process we can see that , The server usually needs to wait for the data to be sent and processed , So the server side ACK and FIN It's usually sent separately , And that's one more than three handshakes .

The interviewer then asked : Why does the client wait after sending the fourth wave confirmation message 2MSL Time to release TCP Connect ?

MSL It means the longest message life . Make sure the server has received our ACK message , After one MSL If you don't receive it after , The server will resend FIN Send the message to the client , At this time, it took another MSL, The client receives... Again FIN After the message , I know what happened before ACK The message is lost , And then send... Again ACK message , It's just the right time to come and go 2 individual MSL.

TCP What is the difference between congestion control and flow control in ?

flow control yes TCP The offer can make 「 The sender 」 according to 「 The receiving party 」 The actual receiving capacity controls the amount of data sent ⼀ Species mechanism . Because if you have no brain to send data to each other , But the other side can't handle it , Then it will trigger the resend mechanism , This leads to the unprovoked waste of network traffic

Congestion control and flow control Different , Congestion control is a global process , Flow control refers to the control of point traffic . At some time , If the demand for a resource in the network exceeds the available part of the resource , The performance of the network is going to go bad . This is called congestion . Congestion control is to prevent too much data from being injected into the network , In this way, the router or link in the network will not be overloaded .

The interviewer asked : How to control the flow control ?
TCP Flow control is realized by sliding window mechanism .
In the process of communication , According to the size of the receiving cache , Dynamically adjust the sending window size of the sender , The receiving window rwnd, The sending window of the sender is the receiving window rwnd And congestion windows cwnd The minimum value of .

The interviewer asked again : How about the specific algorithm of congestion control ?

TCP Four algorithms are used in congestion control of , Slow start 、 Congestion avoidance 、 Fast retransmission 、 Fast recovery :

Slow start : The idea of slow start algorithm is to gradually increase the sending window from small to large when the host starts sending data , That is to say, gradually increase the congestion window value from small to large .cwnd The initial value is 1, Every time after a round of communication ,cwnd double . Because if you immediately inject a large number of data bytes into the network , Then it may cause network congestion .

Congestion avoidance : The idea of congestion avoidance algorithm is to let congestion window cwnd Increase slowly , That is, every round-trip time RTT Just put the delivery on cwnd Add 1.

Fast retransmission and fast recovery : stay TCP/IP in , Fast transfer and block recovery (fast retransmit andrecovery,FRR) It's a congestion control algorithm , It can ** Fast recovery of lost packets .** No, FRR, If the packet is lost ,TCP A timer will be used to request a transmission pause . During the period of suspension , No new or copied packets were sent .

With FRR, If the receiver receives an out of order segment of data , It immediately sends a duplicate confirmation to the sender . If the transmitter receives three duplicate acknowledgments , It assumes that the data segment indicated by the acknowledgment is missing , And immediately retransmit these missing data segments . With FRR, It will not be delayed due to the pause required during retransmission . When a single packet is lost , Fast retransmission and recovery (FRR) Can work most effectively . When multiple data packets are lost in a short period of time , It doesn't work very effectively .

say ⼀ say GET And POST What are the differences ?

Four aspects

  • effect :GET Method means request to get resources from the server . and POST The way is the opposite , It is to URI Specified resource submission data , The data is in the message body in .
  • Parameter position :GET In the URL in ,POST The parameters of are stored in the entity body , also GET Method to submit the request URL The most data in is 2048 byte ,POST There is no size limit for requests
  • Security : stay HTTP In the agreement , So-called 「 Security 」 Means that the request method will not 「 damage 」 Resources on the server .GET The method is safe , Because it is 「 read-only 」 operation , No matter how many times you operate , The data on the server is secure .POST the reason being that 「 Add or submit data 」 The operation of , Will modify the resources on the server , So it's not safe
  • Idempotency : So-called 「 idempotent 」, It means performing the same operation several times , The result is 「 identical 」 Of . Combined with what I just said ,GET The method is idempotent . and POST Methods are not idempotent

Say typing in the browser URL Process to display

Generally speaking, it is divided into the following processes :

  • DNS analysis : Browser query DNS, Get the... Corresponding to the domain name IP Address
  • TCP Connect : The browser gets the domain name corresponding to IP After the address , The browser requests a link from the server , Make three handshakes
  • send out HTTP request :TCP When the connection is established , Browser to server HTTP request
  • The server processes the request and returns HTTP message : The server received the request , It is mapped to a specific request processor according to the path parameters The reason is , And return the processing results and corresponding views to the browser
  • Browser parse render page
  • End of connection

The interviewer asked : What protocols will be used throughout the process ?

First, the browser finds the name of the domain name IP The address process uses DNS agreement Build with server TCP Connected to TCP agreement
establish TCP When the agreement , You need to send data , Sending data is used at the network layer IP agreement IP Packets are between routers , Routing uses OPSF agreement
When the router communicates with the server , Need to put ip Address conversion to MAC Address , Need to use ARP agreement stay TCP When it's done , Use HTTP Protocol access page

HTTP 1.0 、HTTP 1.1、HTTP 2.0 What's the main difference between ?

HTTP 1.0

  • Request and response support HTTP head , The response contains the status line , Added status code ,
  • Support HEAD,POST Method
  • Support transmission HTML Other types of content than documents

HTTP1.0 It uses Non persistent connection , The main drawback is that the client must establish and maintain a new connection for each object to be requested , That is, twice as many documents are requested RTT The cost of . Because there may be multiple objects on the same page , So non persistent connections can slow down the download of a page , And this kind of Short connections increase the burden of network transmission .

HTTP 1.1

  • A long connection Supports long connections .
  • Cache handling stay HTTP1.0 More cache control strategies are introduced on the basis of . stay HTTP1.0 Medium main use header Inside If-Modified-Since,Expires As the criterion of cache judgment ,HTTP1.1 More cache control strategies are introduced, such as Entity tag,If-Unmodified-Since, If-Match, If-None-Match Wait for more alternative cache headers to control the cache policy .
  • Bandwidth optimization : stay HTTP 1.0 There will be a waste of bandwidth in , Mainly because Breakpoint resume is not supported function , The client only needs part of an object , The server sends the whole object over . stay HTTP1.1 The request header in range Header fields , it Support requesting only a part of the resource , The status code returned is 206.
  • Error status : Error status response code is added in error notification management .
  • Host name : Added Host Head treatment , You can pass the host name (hostname).

HTTP 2.0

HTTP2.0 yes HTTP Since the agreement 1999 year HTTP1.1 First update since release , Based mainly on SPDY agreement .HTTP2.0 A big increase in web performance , stay HTTP1.1 On the basis of complete semantic compatibility , Further reduce the network delay . Achieve low latency and high throughput . For front-end developers , Reduced optimization work . Mainly reflected in the following features :

  • The head of compression
  • Multiplexing
  • Binary framing
  • Request priority
  • Server push

SPDY agreement

What is? SPDY agreement ?
SPDY yes Speedy Your nickname , Meaning for “ faster ”. It is Google The development is based on TCP Application layer protocol of protocol . The goal is to optimize HTTP The performance of the protocol , By compressing 、 Multiplexing and priority technologies , Reduce web page load time and improve security .SPDY The core idea of the agreement is to minimize TCP The number of connections .SPDY It's not a substitute for HTTP The agreement , It's right HTTP Protocol enhancements .

The head of compression

stay HTTP/1.1 And before , Request body Generally, there will be a compression coding process of response , adopt Content-Encoding The header field to specify , But have you ever thought about the compression of the header field itself ? When the request field is very complex , Especially for GET request , Request messages are almost all request headers , At this time, there is still a very large optimization space .HTTP/2 For header fields , The corresponding compression algorithm is also used ——HPACK, Compress the request header .
HPACK The algorithm is specifically for HTTP/2 Service , There are two main highlights :

  • The first is to build a hash table between the server and the client , Store the used fields in this table , So when transmitting, for the previous values , Just put the index ( such as 0,1,2,…) Just pass it to the other party , Just get the index and look up the table . This way of indexing , It can be said that the request header field is greatly simplified and reused .
     Insert picture description here

HTTP/2 The concept of starting line is abolished , Start the request method in the line 、URI、 The status code is converted to the header field , But these fields all have one ":" Prefix , Used to distinguish from other request headers .

  • The second is Huffman coding for integers and strings , The principle of Huffman coding is to create an index table of all the characters , Then make the index of characters with more occurrences as short as possible , This index sequence is also transmitted during transmission , Can achieve very high compression .

Multiplexing

It was said before HTTP Team head jam Questions can be used Concurrent connections and Domain name segmentation The way to solve this problem , But it doesn't really come from HTTP Solve the problem on its own level , Just added TCP Connect , Sharing the risk is just . And there are drawbacks to doing so , multiple TCP Connections compete Limited bandwidth , Let the really high priority requests not be prioritized .

stay HTTP1.x in , We often use sprite chart 、 Use multiple domain names to optimize , All because the browser limits the number of requests under the same domain name , When a page needs to request a lot of resources , Team head jam (Head of line blocking) Will result in when the maximum request is reached , The resource needs to wait for other resource requests to complete before continuing to send .

HTTP2.0 in , Based on binary framing layer ,HTTP2.0 You can share TCP Send request and response simultaneously on the basis of connection .HTTP The message is broken down into separate frames , Without breaking the semantics of the message itself , Send out in a staggered way , Reassemble them at the other end according to the stream identifier and header . Through this technology , You can avoid HTTP The old version of the queue head blocking problem , Greatly improve transmission performance .

Binary framing

First ,HTTP/2 Think plaintext transmission is too much trouble for the machine , It's not convenient for computer analysis , Because there will be polysemous characters for the text , For example, whether return line feed is content or separator , We need to use state machine to identify , Low efficiency . therefore HTTP/2 Just change all messages to binary format , All transfers 01 strand , It facilitates the analysis of the machine .

original Headers + Body The message format of is now split into binary frames , use Headers Frame storage header field ,Data The frame stores the request body data . After framing , What the server sees is no longer one by one complete HTTP Request message , It's a mess of binary frames . These binary frames have no precedence , So there's no waiting line , It's gone HTTP The team head block problem .
Both sides can send binary frames to each other , This kind of binary frame bidirectional transmission sequence , It's also called flow (Stream).HTTP/2 Use flow to work in a TCP Connect to communicate multiple data frames , That's the concept of multiplexing .

Maybe you have a question , Since it's a random start , How to deal with these disordered data frames finally ?

The first thing to say is , The so-called disorder order , It means different ID Of Stream It's out of order , But the same Stream ID The frames of must be transmitted in order . When the binary frame arrives, the other party will Stream ID The same binary frame is assembled into a complete request message and response message . Of course , There are other fields in the binary frame , Realize the priority and flow control functions .

Server push

Another thing worth saying is HTTP/2 Server push (Server Push). stay HTTP/2 among , The server is no longer completely passive in receiving requests , Response request , It can also be built stream To send messages to clients , When TCP After the connection is established , For example, a browser requests a HTML file , The server can return HTML On the basis of , take HTML The other resource files referenced in are returned to the client , Reduce client waiting

Request priority

hold HTTP After the message is divided into many independent frames , The performance can be further optimized by optimizing the interleaving and transmission sequence of these frames .

Talk about your right HTTP Understanding of long connection and short connection ?

stay HTTP/1.0 Short connection is used by default in . in other words , Client and server every time HTTP operation , Just one connection , Disconnect at end of task . When a client browser accesses HTML Or other types Web The page contains other Web resources ( Such as JavaScript file 、 image file 、CSS Documents, etc. ), Every time I meet such a Web resources , The browser will recreate a HTTP conversation .

And from HTTP/1.1 rise , Use long connection by default , To maintain connection characteristics . Using long connected HTTP agreement , This line will be added to the response header :

With long connections , When a web page is opened , Transport between client and server HTTP Data TCP Connection will not close , When the client accesses this server again , Will continue to use this established connection .

Keep-Alive Not permanently connected , It has a hold time , Different server software is available ( Such as Apache) Set this time in . To realize long connection, both client and server need to support long connection .

The way HTTP and HTTPS The difference between ?

There are several differences :

  • expenses :HTTPS The agreement needs to reach CA Apply for a certificate , Generally there are few free certificates , You need to pay a fee
  • resource consumption :HTTP It's the hypertext transfer protocol , The message is transmitted in clear text ,HTTPS It is safe SSL Encrypted transport protocol , Need to consume more CPU And memory resources
  • Different ports :HTTP and HTTPS It USES a completely different connection , The ports are different , The former is 80, The latter is 443
  • Security :HTTP It's the hypertext transfer protocol , The message is transmitted in clear text , There is a problem of security risks . and HTTPS Then solve HTTP Unsafe defects , stay TCP and HTTP Between the network layers SSL/TLS Security protocols , To enable encrypted transmission of messages

HTTP Advantages and disadvantages

1. Simple :HTTP The basic message format is header + body , So is the head message key-value The form of simple text , Easy to understand , Lower the threshold of learning and using .
2. Flexible and easy to expand
HTTP All kinds of request methods in the agreement 、URI/URL、 Status code 、 Each component requirement, such as header field, is not fixed , Both allow developers to customize and expand .
meanwhile HTTP Because it's working in the application layer , Then it can be changed at will .
HTTPS That is to say HTTP And TCP Between layers added SSL/TLS Secure transport layer ,HTTP/3 Even put TCP The layer has been changed to be based on UDP Of QUIC.
3. Widely used and cross platform : Internet development to date ,HTTP The range of application of , From desktop browsers to mobile phones APP.

Interviewer asked : Good answer , Talking about shortcomings ?

  • The communication uses clear text ( No encryption ), Content may be bugged
  • Do not verify the identity of the communicating party , So there's a chance of camouflage
  • Can't prove the integrity of the message , So it may have been tampered with

say something URI and URL What's the difference ?

  • URI(Uniform Resource Identifier) yes Uniform resource identifier , You can uniquely identify a resource .
  • URL(Uniform Resource Locator) yes Uniform resource locator , The path to the resource can be provided . It's a concrete URI, namely URL Can be used to identify a resource , It also shows how to locate This resource .

summary

Reference blog
【 Interview questions 】 Computer network -10 A summary of the test questions I often meet
HTTP 1.0 1.1 2.0

原网站

版权声明
本文为[VoldemortQian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131247024496.html

随机推荐