当前位置:网站首页>Reading notes on how to connect the network - tcp/ip connection (II)
Reading notes on how to connect the network - tcp/ip connection (II)
2022-07-04 08:59:00 【Adong】
TCP and IP The protocol carries the lifeline of the entire Internet , This chapter is the core of the book , Mastering these two protocols is also the basis for learning network programming well .
Socket Connect
Socket linking on the surface is to establish a connection , Exchange data , disconnect , Although the details are certainly not that simple , But the general idea is basically the same .
The protocol stack establishes a connection
Here's a premise to remember : When sending a delegate to the protocol stack inside the operating system , You need to call... In the specified order Socket Program components in the library .
establish Socket Most of our agreements are Tcp/ip agreement ,Socket Sending and receiving data is similar to establishing an invisible pipeline between two hosts ,Socket The key to the establishment is to call... In the specified order Socket Program components , The general construction sequence is as follows :
- establish Socket(Socket Similar to the inlet and outlet on both sides of the pipe )
- Bind the socket of the client to the server ( Similar ground pipe )
- Exchange data .
- To break off Socket Connect , Unbind .
The specific flow chart is as follows :
establish Socket
The process is that the application will transfer the control flow to socket Internal and execute the operation of creating socket , After completion, the control process will be handed back to the application .
After creating the socket , Protocol stack You need to return the identifier, that is The descriptor Used to identify which socket is transmitting data , Because we may open many socket connections to visit different websites , The specific effect is that our browser will open many pages , At this point, each page may need to create a socket , At this point, it is necessary to identify and distinguish these socket dependency descriptors .
Bind the socket of the client to the server
The core of connection operation is to call Socket Of connect Connection method , This method needs to specify The descriptor 、 The server IP Address and port number this 3 Parameters .
connect It looks complicated , In fact, the essence is to complete the connection action , A successful connection will IP The address and port number are recorded on the socket .
Descriptor is creating Socket By the time I got it ,IP The address is at DNS The parsing steps are completed , Get IP Then it will be put into a location of the application to replace and save , The port number needs to be provided by the application in advance .
A port can be simply viewed as an entry point to an application ,DNS Analytic IP You can only know where the host is, but it is not clear to which application it is sent to , We can imagine DNS Analyze the coordinates of toll stations on similar maps , But he doesn't know which gate the corresponding data is sent to ).
It can be understood that the port is the toll gate , The computer will require the program to treat the application to preset a clear port to participate in network interaction .
The message
The next operation is to call read and write Function to complete the message passing action , This step is the underlying stream read / write operation .
disconnect
This step needs to be simply understood as that one party needs to initiate the disconnection application browser call read When sending and receiving data, a close request will be received , At this point, after the client confirms the request, it will stop the request and start releasing Socket Connect .
Why can't I use a descriptor to identify the entry of the application ?
- Descriptors are used to create sockets with delegates Used when an application interacts , It is not used to tell the other side of the network connection .
- The client also cannot know the descriptor on the server , The client can't determine a location on the server through the descriptor on the server A socket .〉
Socket In the connection, it roughly introduces how the protocol stack completes the connection with the target server through the network card 、 To break off 、 In the process of sending and receiving data, the following describes the details of each step in sequence .
Let's give a detailed description of the above steps in sequence .
Create socket
First, let's take a look at creating sockets , The following is the content of the protocol stack .
Delegated distribution is split into several parts , The top can be seen as a browser , There are mainly two protocols in the protocol stack TCP and UDP, TCP It is mainly used to send and receive data interactively with the server ,UDP Is used for shorter control data .
IP The protocol mainly controls the network transceiver operation , The main work is to send the split network packets to the target objects of communication ,IP The agreement includes ICMP and ARP agreement , The former informs the transmission process of errors and control information , The latter delivers Ethernet MAC Address .
MAC Address : accord with IEEE All specifications of LAN devices use the same address format , This kind of address is called MAC Address
The driver part is to enable the operating system to normally use the hardware for network transceiver “ Adapter ”, And all the electrical signals are finally completed through the network card .
Socket and protocol stack
The protocol stack actually decides what to do according to the information transmitted by the socket , For example, sending data depends on IP And port number .
With Windows For example , Directly in CMD
Use in netstat
Just operate :
C:\Users\Xander>netstat -ano
agreement Local address External address state PID
TCP 0.0.0.0:49666 0.0.0.0:0 LISTENING 604
TCP 0.0.0.0:49667 0.0.0.0:0 LISTENING 1892
TCP 0.0.0.0:49668 0.0.0.0:0 LISTENING 4508
TCP 0.0.0.0:57621 0.0.0.0:0 LISTENING 22748
TCP 127.0.0.1:1001 0.0.0.0:0 LISTENING 4
TCP 127.0.0.1:1043 127.0.0.1:1061 ESTABLISHED 8452
TCP 127.0.0.1:1043 127.0.0.1:1063 ESTABLISHED 8452
UDP 192.168.159.1:1900 *:* 3060
UDP 192.168.159.1:5353 *:* 5248
UDP 192.168.159.1:58085 *:* 3060
netstat command Of ano The three parameters are mainly used to extend IP Address port and PID Display of , And some implicit possible communications will also be recorded . LISTENING: It means waiting for the other party to connect ESTABLISHED : Indicates that the connection is completed and the data communication operation is performed
The interaction process between socket, protocol stack and application is as follows :
- Protocol stack before socket operation , You need to open up a space in advance to store the necessary information for operating the socket .
- The protocol stack needs to return a descriptor to the application to indicate which one is currently connected “ The Conduit ”.
- After that, if the application needs to interact with the protocol stack, it must carry the descriptor , However, it also saves the protocol stack to know which socket the application will interact with .
Connect to server
The purpose of the connection is to enable two hosts that are no longer in the same place to know each other , At this time, it is inevitable to provide each other with their own information , In this way, the connection can be established correctly and the socket can be used to transfer data .
The meaning of connection
Sometimes people can communicate without using one language , As long as both sides understand , But it doesn't work with computers .
Therefore, the control information of the connection operation shall be determined according to the communication rules , The protocol stack needs to rely on a space to store the necessary data before communication , This memory space is called a buffer .
The connection needs the information that both parties tell themselves , So there was no data interaction at the beginning of the connection , Because it is TCP It is a full duplex protocol. Both the client and the server need to establish sockets , However, both parties do not know who to connect with , So you need to open up a space on the client and server to store each other's IP And ports and other necessary transmission information .
In order that both parties can communicate normally , You can also design the control information processing mode of protocol stack and socket according to your own system , The network communication design adopts Control information So that different computers and systems can know each other .
The so-called control information can be considered as a kind of General language , As long as the header information conforms to this control information specification, it can be recognized by other computers .
There are two types of control information :
- Control information exchanged between client and server , It is mainly used in the whole communication process , These are in TCP agreement To regulate . The example of life is before we talk to others , Both sides need to know each other's phone number and basic identity .
- The information stored in the socket to control the operation of the protocol stack , This information is mainly used to transmit data , It usually needs to include communication control information and data blocks , The socket needs to know what type of data is sent through the control information , Then the protocol stack can cooperate to process the data .
There is no specific data at the beginning of transmission , Usually an empty message header , So this control information is also called Protocol header , For example, as mentioned below TCP Head ,IP Head .
The first category :TCP Header format
The second category : Information in the socket
The actual operation of the connection
The actual operation of the connection is mainly to call CONNECT
function , The agreement will first be passed to TCP modular , adopt TCP Modules exchange headers for obtaining control information , To understand the specific socket information to be connected , Then put the head SYN The bit is set to 1, Indicates that... Can be connected .
TCP A header representing the connection control information is created at the module , Then the message is passed on to IP Module to delegate sending .
Three handshakes
After exchanging header information , Then there is the common TCP Three handshakes :
- First step : The client actively opens TCB port , The server is turned on passively TCB port . The initiator carries a SYN sign , And carry one ISN Serial number Seq=x, But it should be noted that the process of the first step ISN The serial number is hidden and passed ( Because no data is passed ), Because if there is no data exchange in the request, it will not be displayed . The client sends SYN After the command, enter the setting SYN=1, And set up SYN-SENT( Sync - Sent state ).
- The second step : The server received a client TCP After the message , Will also be SYN=1, And send back a new ISN Serial number ack=x+1, And will ACK=1 Show that you have received , Then send back your new serial number in the return parameter to indicate your confirmation request Seq=y, Set status to SYN-RCVD( Sync received ) state ,( Indicates that the serial number you want to receive is xxxx1522), Finally, it is also specified MSS.
- The third step : After the client receives the confirmation message from the server , You also need to return a confirmation message to the server , Confirmation message ACK=1, And send back the information passed by the server ISN Serial number +1(ack = y+1), And my own ISN Serial number +1(Seq = x+1), here TCP The connection enters the connected state ,ACK It can carry data , However, if no data is carried, the serial number will not be consumed .
- The last step : When the server receives a confirmation from the client , Also entered connected state .
After three handshakes, the connection is established , Data can be transferred until the connection is disconnected .
Sending and receiving data
There are two key points in sending and receiving data :
- The first point is that sending and receiving data does not care about the format of the data , Instead, it identifies what type of data it is based on the header information , For the protocol stack, the received content is binary data .
- The second point is to use buffer to reduce frequent data transmission and improve transmission efficiency .
How to control the size of the buffer ?
- The data length of each packet , The protocol stack is called MTU To judge , however MTU Refers to the total length , Get the real data length after removing the header information MSS.
- Time , This time refers to the fixed time content regardless of whether the buffer has reached MSS Length the time that data must be sent , The purpose is to prevent request delay caused by long waiting time .
A term is used to explain : MTU: The maximum length of a network packet , Ethernet is generally 1500 byte .MSS: After removing the head , What a network packet can hold TCP Maximum length of data .
But these two factors can not completely determine the efficiency balance of sending and receiving data ,TCP The protocol does not specify how the protocol stack is balanced , It depends on how the operating system decides .
In fact, the protocol stack keeps sending and receiving data , It is not mandatory to deal with it in accordance with the provisions of the agreement , Instead, it gives the application some controllable options , For example, the browser is an application that requires real-time performance Buffers are usually not used .Http Request split
General situation http The content of the request response can be completed through a network packet , But for POST Data submission of large forms such as request will usually trigger TCP Unpacking operation .
Unpacking is based on MSS The parameters of , The send buffer will split a data exceeding the length of one request into multiple packets according to this parameter , But because they actually belong to the same data , After splitting, all packets need to add the same header .
Be careful :TCP It's a byte stream oriented protocol , It's a string of data that has no boundaries , Ben didn't “ package ” The concept of ,“ Sticky package ” and “ unpacking ” One is to help us understand these two phenomena vividly .
TCP Sticky package
TCP In addition to unpacking, it also includes the operation of sticking packages , The so-called sticky bag means TCP In the protocol, several packets of data sent by the sender are glued into one packet when received by the receiver , From the perspective of receiving buffer, the head of the latter data is immediately followed by the tail of the previous packet .
Solve the sticky package 、 Unpacking strategy ?
Sticking and unpacking need to solve the fundamental problem that is easy to cause half package reading and writing , There are many solutions , The main strategies are basically mentioned in many online materials , Here is the conclusion :
- Request message fixed length , If the buffer is not full , By making up 0 The way to reach the length , Prevent sticking and unpacking .
- Add a carriage return line break at the end of the packet to split , for example FTP agreement ;
- The message is divided into header and body , Save the entire length of the message header , Only when a message of sufficient length is read can a complete message be read ;
- Through the custom protocol to stick and unpack .( Almost no use )
ACK No. to confirm network packet sending and receiving
ACK In addition to confirming whether the other party has received the request during the three handshakes , It can also be used as a basis for judging whether the received packets are complete , In the process of data transmission , The receiver will add up the length of the data received so far , Calculate how many bytes have been received , Then write this value to TCP The head of the ACK Number to the sender , To show how much data you have received , If there is a lack of data in the middle, the server can retransmit .
Of course, it's just ACK No. is not a complete reference , And only use ACK No. is to consider only One way transmission The situation of , however TCP It's full duplex protocol , Unable to determine which party the data receiver is from .
It's easy to solve this problem , In fact, during bidirectional data transmission, both parties will calculate an additional serial number , The serial number is actually a set of random numbers , After receiving the data, the receiver needs to change the serial number every time +1 It is returned to the sender to indicate that it has received all data before the serial number .
adopt ACK+ Serial number To ensure correct data transmission , In this way, other network communication components do not need additional failure compensation mechanism , If packet loss or incomplete data is found , Retransmit and resend directly according to the serial number .
Factors affecting data transmission
The main influencing factor is return ACK Waiting time for No .
If ACK No. 1 has not responded to the other server , It will inevitably affect the efficiency of the entire network transmission , If the next data is ready and the last return packet is not sent back , It is easy to cause network congestion , The other party is too late to get the right result .
The complexity of the network environment , This waiting time cannot be fixed , therefore TCP Dynamic time method is used for adjustment , The specific adjustment method is to use The sliding window .
The sliding window
The sliding window : It means not waiting ACK When the result is returned, both parties directly send data to each other without interruption .
Both parties need to return through their respective buffers ACK Information , However, if the data is sent without restriction, the data cannot be processed and packet loss will occur , So the key to sliding windows is The receiver needs to tell the sender how much data it can receive at most .
The details of the sliding window are better understood through a picture :
About the receiving volume of the receiver , The maximum amount of data that can be processed is determined by the buffer size . Also note that the following figure has only one-way parts , In fact, it is similar for both directions .
Secondary factors affecting data transmission : return ACK Number and time to update the window .
Just remember one conclusion about this , The receiver is sending ACK When the number and window are updated , The package will not be sent out immediately , But will wait for a period of time , Wait until the other notifications are merged , because ACK No. indicates the data volume of the received packets , Using this delayed sending method can also prevent too many update packets from appearing .
The details of the final protocol stack sending and receiving data are as follows :
- The protocol stack will TCP perhaps IP The header parses the content , If you confirm that you have received the data, return ACK + Serial number .
- The protocol stack will put the data block into the buffer for storage , Use the sliding window feature to process the data in order and hand it to the application program for processing .
- The protocol stack will copy the received data to the memory address specified by the application , Then return the control process to the application
disconnect
The disconnect section contains the operations of disconnecting and deleting sockets , Disconnect is the classic four wave operation , To delete a socket, note that the protocol stack does not specify the closing time , But usually the socket is deleted after a few minutes .
Four waves tcp Connect
- First step ( client ):TCP Send a message to release the connection , Stop sending data , Release header , hold FIN=1, Send the serial number at the same time , According to the serial number of the last transmission +1 delivery Seq = t + 1( Because the figure below shows four waves immediately after connection , So the serial number hasn't changed ), At this time, the client waits for termination 1 The state of . Be careful FIN The serial number needs to be consumed without carrying data .
- The second step ( Server side ): Server sends back confirmation message , Send confirmation message ,ACK=1, And send back the serial number +1 Comes back (ack = t + 1), Then bring your serial number Seq = y, At this time, the server enters CLOSE-WAIT state ( Turn off wait ),TCP At this time, the server needs to stop the upper application. The client requests release from the server , be in Half closed Stage , At this time, the server can still send data to the client and the client needs to receive and process , Turning off the wait state means that the whole state needs to last for some time .
- The third step ( client ): The client receives the confirmation request from the server , At this point, the client enters the FIN-WAIT-2 Stop waiting 2 The stage of , Wait for the release message of the server .( There are also some data that the server has not sent out that need to be processed )
- Step four ( Server side ): The server processes the final data , Send release message to client ,FIN=1,ack=t + 1, Due to the need to send the remaining data , Suppose you need to bring your own serial number after processing Seq=w, Server entry Final confirmation status , Wait for the client to confirm .
- Step five ( client ): After the client receives the message , Send a confirmation ACK=1,ack=w+1, Your serial number is Seq = t + 1, At this point, the client enters TIME-WAIT( Time waiting state ), At this time, the client still does not release , Must go through **2 * MSL( Maximum message life )** after , The client is removed TCB After that, I entered CLOSED state .
- Step six ( Server side ): The server enters immediately after receiving the request from the client CLOSE state , Also revoke TCB, End this TCP The connection of .( End of server TCP Connect earlier than the client )
There are many details about the interaction process between socket and protocol stack and the other server , It can be found that three handshakes and four waves are actually only a small part of the network connection , Finally, there is a simple summary diagram from connecting services to data sending and receiving to disconnecting , It is suggested to be used as a general process reference :
IP And Ethernet transceiver operation
The above part is close to TCP Protocol related operations ,TCP In fact, you need to IP Cooperation of modules , Before understanding how the two modules work together, you need to understand how the complete network package is composed .
The composition of the bag
For any network packet , Have the outermost abstract concepts , That's it Head and data Two parts
The upper part has a diagram like this , Inside the socket TCP data , Here we need to pay attention to TCP In front of the control information are Ethernet and IP Control information , For network packets that only transmit control information, although there is no data part , However, the header information of the protocol can be regarded as the data part .
It can also be found from the following figure , All network packets must be delegated to Ethernet and IP Control information can be transmitted .
Split the network packets storing data , You can see the following TCP/IP Package structure :
We can simply regard the header and data as the usual express , The head is a single face , Indicate where to go from , And then this “ Courier ” It will judge the direction of transmission through the table lookup operation of the network forwarding device .
What is the forwarding device ? It is suggested to look at the basic concepts of network transmission in Chapter 1 , Let's just mention it here :
- The router judges the location of the next router according to the target address
- The hub transmits network packets to the next route in the subnet
But in fact, hubs and routers have different responsibilities , The hub is responsible for managing the Ethernet rule transport package devices , Router Management IP Forwarding rules , So the above two steps can also be understood as follows :
- IP The agreement judges the next according to the hierarchy rules IP Forwarding device .
- The Ethernet protocol of the subnet is forwarded to the next forwarding device .
In fact, the head should be divided into MAC Head and IP Head . Why split the header into two protocols ? In fact, it is to enable the replacement between protocols , such as MAC It can be replaced by LAN 、ADSL、FTTH. At the same time, because of such a huge network architecture as the Internet , A more detailed division of labor is needed .
Summary
In fact, the encapsulation of network package should cover three parts :
The first part is TCP Modules organize header information and packets ( Of course, there may be no data but control information ).
The second part is to put the whole TCP The module is plugged into IP Behind the module , Then send it through the network card .
The third part is in IP Add... Before the module Mac Information .
The key : Whether the packet to be sent or received is a control packet or a data packet ,IP The sending and receiving operations of various types of packets are the same .
A term is used to explain ADSL[1]: It can be understood that in the past, broadband used dial-up connection to access the Internet . Asymmetric ** Digital subscriber line **[2]( English :Asymmetric Digital Subscriber Line) also called Asymmetric digital subscriber loop (Asymmetric Digital Subscriber Loop), abbreviation ADSL.ADSL It is a data transmission technology relying on copper telephone lines that is faster than traditional modulators . FTTH[3]: In fact, it is now optical fiber communication . Fiber to home ( English :Fiber To The Home, abbreviation :FTTH) It's a kind of Optical fiber communication [4] Transmission method of . It's to put Optical fiber [5] Receive user [6] At home ( Where the user needs ). This optical fiber communication mode and strategy are similar to FTTN[7]、FTTC[8]、HFC[9](Hybrid Fiber Coaxial) And so on , They all rely on traditional metal wires , Include Twisted pair [10] And Coaxial cable [11] etc. , do “ The last mile [12]” Information transmission of .
IP Protocol header
IP Similar to the number on the express , So actually IP The module cannot decide to choose the correct address by itself , Even if the application passes TCP tell IP The address type sent is a wrong address ,IP We can't fix it by ourselves. We can only do it .
It can be seen from this feature that IP The head is a bit like a courier , It is the same with the wrong address of the online shopping merchant or the wrong address we fill in , You can't blame the courier .
IP The composition of the protocol header is similar to the following structure , Be careful IP The length of the address is fixed 32Bit The space occupied by .
Here we need to pay attention to “ The sender's IP” The address does not refer to the computer IP, It is Refers to the corresponding network card IP, because IP Not with the computer but with the network card , When a computer has multiple network cards, there will be multiple IP.
So how to determine which network card the packet is sent to ? This involves IP Rules of agreement , Whether it is the forwarding of the router or the processing of the protocol stack, it needs to follow IP The agreement is transferred to the next user .
Querying which network card is assigned to is different in different operating systems , To query the sender, you need to query which network card sent the packet to the router , This action simply depends on the router IP Address and network card IP compare .
stay windows You can use the command
route print
View route information .
obtain IP And network card , You also need to know the protocol to which the package belongs , The content of the delegation is fixed , such as TCP A module is 06,UDP Namely 17, Most requests are HTTP, Use TCP Transmission by .
Ethernet Mac Head
TCP/IP Modules can only understand each other at the transport layer , But down The link layer Ethernet doesn't work with the same rules , So add... To the head TCP/IP Behind the head of , You also need to add Mac Head ,Mac The header contains the sender and receiver Mac Information , This can be simply understood as Mac and IP Has a similar effect , however Mac The head is 48Bit, and IP The head is 32Bit.
Note that the etheric type is Mac The type of real data behind the package , If it is IP Namely IP agreement . In addition, you should pay attention to sending Mac Before packaging to the recipient , Because I don't know each other's Mac Address , So you need to do one more step of querying .
Be careful IP Module according to routing table Gateway The contents of the column determine who should send the package to .
Here is Mac The composition of the head :
Inquire about Mac Address
Query the other party's Mac The address needs to use ARP( Address Resolution Protocol, Address resolution protocol ),ARP Find the target address by broadcasting , Broadcasting is literally sending messages to all other Internet users , Wait for the answer .
In order to prevent each query from bringing ARP The data of , So there's a piece ARP The cache of is dedicated to caching this address , But you need to pay attention to this cache and IP Modular IP The address is the same , After a while, it will be ARP Cache obsolete , But if IP Just the change may lead to ARP The cache is not updated in time, resulting in network exceptions .
Actually the whole work is done by IP Module complete , although Mac Address is necessary for Ethernet data transmission , But it actually makes IP It is advantageous for the module to be responsible for these tasks .
Why Ethernet is needed ?
# With IP Address , Why use MAC Address ?
Basic knowledge of Ethernet
First, let's take a look at the basic development of Ethernet , Although the design structure is becoming more and more sophisticated , But the work done in essence has not changed .
In essence, the early prototype of Ethernet can be seen as a network cable and a device for transceiver , After the network signal is sent, it finally reaches all devices through broadcasting , Send and receive messages at the beginning so that others can know who the message will eventually be sent to , stay Mac The head contains “ Shipping address ”, The specific types of messages sent can be sent through the above “ The type of ether ” Judge .
Ethernet will replace the backbone network cable in the subsequent development Replaced with a relay hub , The transceiver becomes twisted pair , Although the form has changed , But the nature of the work has not changed .
Ethernet to the modern, and finally by the switching hub to complete all the operations , And only the requestor and the receiver can communicate with each other in network requests , Increased integration and network transmission security .
But no matter how Ethernet develops, its nature has never changed :
MAC The address represents the destination , Use the sender MAC The address identifies the sender , Use the Ethernet type to identify the contents of the package .
IP Module light conversion ( Or electricity ) The signal
Network signaling depends on the network card , But the network card can't be used when it is plugged in , You also need to rely on initialization and drivers to complete the operation , Drivers and initialization operations are common in many other computer hardware , But Ethernet has a unique driver operation , That is to control the Ethernet transceiver operation MAC Send and receive in the middle MAC Address .
Another feature of the network card is the network card ROM The world's only MAC Address , This was decided when the network card was produced .
So you can see the final completion IP The key to data conversion is the driver , Saved in the network card MAC The address will be read and assigned to by the network card driver MAC modular .
Control information of network packets
MAC The module works on the network card MAC After the packet sends the request command ,MAC The work of the module is to divide the network packets “ The border ”.
In order to delimit the boundary ,MAC The module adds three control messages :
- Headlines : It's a string of images 10101010… such 1 and 0 Alternating bit sequences , The length is ** 56 The bit **, Its function is Determine the reading time of the package .
- Start frame delimiter (SFD): Determine the starting position of the frame , It is mainly the auxiliary electric signal to segment the message header and the real network packet boundary , And determine the limit of each bit .
- FCS: Check the waveform disorder caused by noise during packet transmission 、 Data error , It's a string 32 A sequence of bits , Is through a formula to the package from beginning to end All the contents of the
The response content is transmitted from IP to TCP
When the server receives the network packet , First, the protocol stack will determine the Ethernet type of the Ethernet header , Found to be 0800 by TCP/IP agreement , Next is IP Modules work , The first is to check IP Whether the head is correct ,IP Is the address correct .
If the recipient is window client , Because packets will not be forwarded , If you find that the package was not sent to you , Would call ICMP The message is returned to the request sender ,IMCP The message format is as follows :
In addition, the received network request may appear because the packet size is too large IP Fragmentation , The split bag will be in IP Mark in the flag field of the header ,IP The module will temporarily store the fragmented packets in the internal memory space , Wait for the same ID All packets received in the buffer are spliced .
How to ensure that the splicing sequence is correct ? You can see the above IP There's another one on the head Slice offset (fragment offset) Field , it Indicates the position of the current partition in the whole package .
IP After the module completes the data fragment reorganization , Give the packet to TCP Module operation ,TCP The requestor and the receiver will also be checked again IP Information , And get the port number to find the corresponding socket , After finding the socket, different operations are performed according to the type of application , This process may be to establish a connection , It can also be used to read and write application data .
Here you may feel IP The check is not IP Module operation ,TCP Go and see IP Is the module information “ Ultra vires ” 了 ? In fact, this is a violation of performance overhead “ Dimitar's law (Law of Demeter)” A special case of . because TCP Modules need to be used frequently IP Module information , If the interactive transmission between data is always required, the performance will be greatly affected .
UDP Protocol transceiver operation
TCP/IP In order to ensure accurate data transmission, a series of complex modules and processes are used to ensure complete data transmission , But sometimes some applications will abandon the use in order to ensure efficiency TCP This complex mechanism .
UDP The key point of the protocol is to solve all the data in one package as far as possible ,UDP No, TCP Receive confirmation of 、 Window and other mechanisms , Therefore, before sending and receiving data, it is not Need to exchange control information .UDP The implementation of is very simple, only the application needs to add the header , Give it directly to IP The module is completed , The receiver only needs to check IP The sender and receiver of the header IP Address information , And then from UDP Port number found , Finally, find the socket information and send the data to the application .
because UDP The stability of the transmission is not guaranteed, so it doesn't matter whether the packet is received or not , As long as the other party doesn't respond, just resend the package . There are many scenarios that do not need to ensure transmission stability , Such as chat data 、 Audio and video information , It doesn't matter if you lose a little , It's just a catnap at best .
The key :UDP The maximum length of data that can be sent is IP Subtract... From the maximum length of the package IP Head and UDP Head The length of . Generally speaking IP The head is 20 byte ,UDP The head is 8 byte , therefore UDP The maximum data length of is 65 507 byte .
Here is UDP The head information of :
Summary
In Chapter 2, we understand Socket The steps and general details of the link , In addition, two important protocols of the whole Internet are introduced TCP The protocol and IP agreement , In the two protocols, we need to focus on the head design ,IP Module to complete TCP Module data ,TCP Data encapsulates application data , Then we need to cooperate Mac The Ethernet module completes the final encapsulation of the network package , When all the preparations are finished , The network card and the driver send the whole package , So you can actually see that it is the network card and driver that decide whether you can surf the Internet ( It's not bullshit ), But these content belong to different levels of content , It needs to be digested one by one .
It introduces TCP after , In the last part of the second chapter, we briefly mentioned UDP agreement ,UDP It's a simple, violent agreement , The purpose of the design is to make all the data complete in one package as much as possible , So it doesn't need a link or secure data transmission , If the data is lost, it can be transmitted directly ,UDP It is also widely used , Such as game , video , Music and other data transmission , Most of the time, it doesn't matter to lose a little , Because even if these data are found, it is meaningless .
TCP/IP Connection is only a small part of Internet data transmission , But it's really the core part , Although there are still details about Ethernet, network card and how to access the Internet for network communication , But only a deep understanding TCP/IP Protocol can understand how the whole Internet interacts and transmits data .
Reference material
[1]ADSL: ADSL/ADSL.md
[2] Digital subscriber line : https://zh.m.wikipedia.org/wiki/ Digital subscriber line
[3]FTTH: FTTH/FTTH.md
[4] Optical fiber communication : https://zh.m.wikipedia.org/wiki/ Optical fiber communication
[5] Optical fiber : https://zh.m.wikipedia.org/wiki/ Optical fiber
[6] user : https://zh.m.wikipedia.org/wiki/ user
[7]FTTN: https://zh.m.wikipedia.org/wiki/FTTx
[8]FTTC: https://zh.m.wikipedia.org/wiki/FTTx
[9]HFC: https://zh.m.wikipedia.org/w/index.php?title=HFC&action=edit&redlink=1
[10] Twisted pair : https://zh.m.wikipedia.org/wiki/ Twisted pair
[11] Coaxial cable : https://zh.m.wikipedia.org/wiki/ Coaxial cable
[12] The last mile : https://zh.m.wikipedia.org/wiki/ The last mile
边栏推荐
- Démarrage des microservices: passerelle
- Awk from entry to earth (14) awk output redirection
- 20220701 Barbalat引理证明
- ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
- awk从入门到入土(8)数组
- A subclass must use the super keyword to call the methods of its parent class
- Live in a dream, only do things you don't say
- Awk from entry to earth (12) awk can also write scripts to replace the shell
- Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
- Internal learning
猜你喜欢
How to solve the problem that computers often flash
【无标题】转发最小二乘法
awk从入门到入土(12)awk也可以写脚本,替代shell
Nurse level JDEC addition, deletion, modification and inspection exercise
Educational Codeforces Round 115 (Rated for Div. 2)
Target detection -- intensive reading of yolov3 paper
Relationship and operation of random events
Openfeign service interface call
没有Kubernetes怎么玩Dapr?
How to solve the problem of computer jam and slow down
随机推荐
微服務入門:Gateway網關
Lauchpad X | 模式
Clion console output Chinese garbled code
Implementation principle of redis string and sorted set
What if the wireless network connection of the laptop is unavailable
Awk from getting started to digging in (9) circular statement
C语言-入门-基础-语法-[运算符,类型转换](六)
Codeforces Round #793 (Div. 2)(A-D)
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
C # implements a queue in which everything can be sorted
Use Alibaba cloud NPM image acceleration
C language - Introduction - Foundation - syntax - data type (4)
Explain TCP protocol in detail three handshakes and four waves
Leetcode topic [array] -136- numbers that appear only once
20220701 Barbalat引理证明
根据数字显示中文汉字
LinkedList in the list set is stored in order
How can we make a monthly income of more than 10000? We media people with low income come and have a look
[error record] no matching function for call to 'cacheflush' cacheflush();)