当前位置:网站首页>[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect

[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect

2022-06-26 13:15:00 Currybeefer

1. Protocol stack
The following is the network control software in the operating system , That is, the internal diagram of the protocol stack . This diagram just shows the general rules , No details involved , For reference only .

 Insert picture description here
Let's look down , The top-level web applications are the ones we use everyday, such as browsers , E-mail and other programs . These applications will send and receive data to the lower layer for processing .
The lower layer of the application is socket library , This is a link library . stay linux Next ,socket There are both dynamic and static libraries . The library contains the parser mentioned earlier ( Used to direct to DNS The server sends a query ).

Then it goes inside the operating system , This is also the location of the protocol stack . among TCP Protocol procedure section and UDP The protocol program part depends on the lower layer IP The protocol program controls the sending and receiving of network packets .IP Protocol program is responsible for sending network packets to communication objects .IP The protocol procedure section also includes ICMP Protocol procedures and ARP Protocol procedure .ICMP It is used to inform the error information and various control information generated in the process of network packet transmission .ARP For use in accordance with IP Address query corresponding MAC Address .

2. Socket

So what exactly is a socket ? First look at the definition given in the encyclopedia

So called socket (Socket), It is the abstraction of two-way communication endpoints between application processes on different hosts in the network . A socket is the end of process communication on the network , Provides a mechanism for application layer processes to exchange data using network protocols . In terms of position , The socket is connected to the application process , Lower network protocol stack , It's an interface for applications to communicate through network protocols , It is the interface between application program and network protocol stack

actually , There is a memory space inside the protocol stack for storing specific control information , Here, the internal information used to control the network communication operation is recorded . For example, the communication object IP Address , Port number, etc . This memory space containing control information is the socket entity .

When the protocol stack performs operations, it needs to refer to the control information in the socket . For example, when the protocol stack wants to send data , You need to take a look at the communication object written in the socket IP And port number , To successfully send the information .

Want to see what the socket looks like ? Enter... Directly on the control line netstat You can look at :

 Insert picture description here
There can be multiple sockets in the protocol stack , They have their own unique socket descriptor , The protocol stack distinguishes different sockets by descriptors .

3. Browsers use TCP The specific process of sending and receiving messages
 Insert picture description here

Pictured , Call the parser to parse DNS We have already talked about the process in the first chapter , Let's directly look at the browser's information about the server after parsing IP After the address .

First step : Create socket

 Insert picture description here

First , Browser call Socket Library request to create socket , The protocol stack performs the operation of creating sockets according to the request , It allocates a memory space to the socket created , Then tell the descriptor of the socket to the browser , After the browser receives the descriptor , Even if the creation is complete .

In a connection , When the browser wants to entrust the protocol stack to send and receive information, it only needs to provide this socket descriptor , The protocol stack knows which socket information to refer to , Where was the message sent .

The second step : Establishing a connection (TCP Three handshakes )

up to now , The browser has just delegated the protocol stack to create a socket , There is nothing in it , So the browser will first set the server's IP The address and port number inform the protocol stack , Let the protocol stack fill in . But the socket is far more than just storing the destination address and IP Address and port number information , Other information, such as the specified serial number, must be obtained from the server .

At the same time , The socket on the server side is still blank ( When the server starts, it has already created a socket to wait for the connection ), There is no necessary information about the current browser at all . This is normal , After all, the two have not formally communicated .

So before the formal transmission of data , The socket of client and server must have each other's information first , Otherwise, I don't know who to send the data to , How to transmit . This is the meaning of connection : The communication parties exchange control information with each other .
 Insert picture description here
The actual process of connection is as follows , At the same time, it's also TCP Three handshakes , The suggestion goes with TCP Shake hands three times to see

 Insert picture description here
 Insert picture description here

 Insert picture description here

What it looks like after connection
 Insert picture description here

The third step : Start data transfer

When the connection is established , You can send and receive data . The application will call write The instruction gives the data to be sent to the protocol stack , Here the browser will http The request message is sent to the protocol stack .

The protocol stack doesn't care what you send , In the eyes of the protocol stack, this is just a pile of binary data . To control transmission efficiency , Generally, the protocol stack will put these data in the internal send buffer , Then send the data at your own pace ( Of course, you can also make the protocol stack send some data immediately ). For data that exceeds the maximum packet size , When the protocol stack is transmitted, it will also be split into appropriate lengths for transmission .

At the time of sending , In the protocol stack TCP The module adds... To the data TCP Head , And mark the port numbers of the sender and receiver according to the control information recorded in the socket .IP Modules will also be preceded by IP Head and MAC Head . Pictured

 Insert picture description here

Now let's see TCP The look of the head

 Insert picture description here

We know TCP The protocol is a reliable transmission protocol , What exactly does this mean ? actually TCP Header ACK And serial number SYN Is the key to this .ACK Is used to indicate that the currently received byte is the number of bytes received .SYN Indicates the number of bytes from the beginning of the current network packet .

When transmitting, we rely on these two things to confirm whether the data has been correctly received . Before getting confirmation from the other party , The sent packets will also be saved in the send buffer , If the other party does not return the corresponding network packet ACK Number , Then resend these packets .

Be careful , To prevent cyber attacks ,SYN Often not from 1 At the beginning , Instead, the sender randomly assigns a data as SYN The initial value of the , And inform the receiver during the connection phase . because TCP Data sending and receiving are bidirectional , Therefore, both the client and the server need to specify an initial value and inform each other during the connection phase .

besides , In the process of data transmission, the sliding window method will be used , That is, do not wait one by one ACK Packet response acknowledgement , Continue sending network packets . This can improve the transmission efficiency .

 Insert picture description here

Step four : receive HTTP response message

The browser sent data , Naturally, you have to wait for the data sent back by the server . At this stage, the browser will call read, Then the control flow is transferred to the protocol stack , Wait until the protocol stack receives the response data from the server , Will do the following .

First, the protocol stack will check the received packets TCP Head content , adopt SYN Determine whether there is data loss , If not, the corresponding ACK Number , Then connect the data in the package in order , Restore to original data , Copy to the memory address specified by the application , Finally, the process is exchanged to the application to continue execution .

Step five : disconnect (TCP Four waves )

All data sending and receiving ends , Then it's time to say goodbye . Either the server or the client can initiate disconnection , The design of the protocol stack also allows applications to initiate disconnections .

The following is the specific process of disconnecting :
 Insert picture description here

 Insert picture description here

Step six : Delete socket

After the communication is over , The socket on the client side is no longer used , You need to remove . But the socket will not be deleted immediately , Instead, wait a few minutes and delete . This is to prevent some accidents . For example, the last one returned by the client ACK The number was lost in the middle , Then the server will resend FIN, At this point, if the socket has been deleted , Misoperation will occur , Make the connection not really disconnected .

原网站

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