当前位置:网站首页>[how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)
[how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)
2022-07-02 17:40:00 【Currybeefer】
1. Server architecture
When the packet arrives Web After the server , The server will receive the packet and process it . There are many kinds of servers , Its hardware and operating system are different from the client . But network related parts , Such as network card , Protocol stack ,Socket Libraries are no different from clients . Whether hardware or OS How to change ,TCP and IP It's all the same .( Of course Socket The usage of the library and the structure of the server program are still different )
The program structure of the server is shown in the figure , We can divide the program into two modules , Waiting for connection module a And the module responsible for communicating with the client b.

When the server starts, it will run the connection waiting module a, This module is responsible for creating sockets , Then enter the suspended state of waiting for connection . When the client initiates a connection ,a The module will resume operation and receive connections , Then start the communication module b, to b Complete the connected socket . then b The connected socket will be used to communicate with the client . Every time there is a new client connection , Will start a b, So the client and b It's a one-to-one relationship .
2. The process of establishing a connection with the server
Next, let's look at the process of the server accepting connections .
First a The module exists when the server program starts , First , The protocol stack will call socket Create socket , Write the specified port number to the socket . Next , The protocol stack will call listen Write the control information of waiting for connection status to the socket , In this way, the socket enters the state of waiting for connection . Then the protocol stack will call accept To accept the connection , Then immediately pause the program . Wait for the network packet to really arrive , Continue the process .
Once the client package arrives , The program will return the corresponding package and then accept the connection operation .A The module will make a copy of the socket waiting for connection , Connect the client socket with this copy . Then start b modular , Give a copy to b modular . Then the data receiving and sending operation is carried out .
Why make a copy of the socket instead of giving it directly to b Well ? Because if you don't create a new , Then give this socket to b After module , There will be no socket waiting to connect , If other clients initiate the connection, they will encounter problems .
So since sockets are copied from a mother , Then the port numbers of these sockets must be the same . After a network packet arrives , If the protocol stack only looks TCP The slogan of the head receiver , You can't tell which socket to give the package to .
The solution is , It's not just the slogan of the receiver , The protocol stack should pass through the client IP Address , Port number , Server side IP Address , Port number to jointly determine which socket to give the packet . As shown in the figure

3. The server receives the operation
5. 
The following describes in detail the operation of the server when receiving network packets , Suppose this is a TCP Network package .
MAC modular
The first is in the network card MAC Module responsible , After the network card receives the signal , Extract the clock signal according to the change of the header signal , Then use this clock signal to restore digital information .
Next, look at the frame check sequence at the end of the package FCS To check for errors , If it is found that the data has been distorted due to noise and other effects , Then discard the network packet .
When FCS Check by , Now check MAC Receiver in header MAC Address , See if this bag is sent to you .
If it's your own , Save the information in the buffer of the network card , Call interrupt , Give Way CPU To handle the received network packets .CPU according to MAC The Ethernet type field in the header determines the protocol type , here , The value of a type indicates IP agreement , So it will call TCP/IP Protocol stack , And forward the package to it .
IP modular
And then it goes to IP The module starts working .IP The module will check IP Whether the header format is standardized , Then look at the receiver IP Does the address say yourself . When the server has a packet forwarding function similar to that of a router , For packets that are not their own, they will also forward packets according to the routing table like the router .
If it's me , Next, check whether the package is partitioned ( In pieces IP There will be a sign in the header ), If it is fragmented, it will be temporarily stored in memory , When all the pieces arrive, they will be assembled together . Then check that the IP Protocol number field in the header , Found to be TCP agreement , Is passed on to TCP Field handling .
TCP modular
If TCP The head of the SYN by 1, Means that this is a package that initiated the connection , This is a TCP The module will check TCP The port number of the receiver in the header , Confirm whether there is a socket with the same port number and waiting for connection . If not, the package containing the error notification is returned to the client .
If there is a socket , Then make a copy of the socket , Then send the sender's IP Address , Port number , Initial value of serial number , Necessary parameters such as window size are written into the copy , Allocate memory space for send buffer and receive buffer . Then generate the ACK Number , The initial value of the sequence number used to send data from the server to the client , Represents the window size of the remaining capacity of the receive buffer , And use that information to generate TCP Head , entrust IP Module to client .
After the package arrives at the client , The client will return a message indicating the receipt of the confirmation ACK Number , When this ACK After No. is returned to the server , The connection is done . This is what the server program should call accept The pause state of , When the descriptor of the new socket is handed over to the server program , The server program will resume .

If TCP At this time, the module receives a packet , Then check IP Module and TCP Both sides in the module IP Address and port number information , Then find the socket where all the information matches . Then calculate the sequence number of the packet that should be received according to the sequence number and data length of the previous one stored in the socket , Then with TCP Compare the serial number of the head , See if there is any packet loss . If always , be TCP Will take data out of the package , Put it in the receive buffer , Connect with the last data block received in the buffer .
after TCP A confirmation reply packet will be generated , Calculate according to the package serial number and data length ACK Number , entrust IP The module is sent to the client .( Before returning this package , I will wait for a while , See if it can be merged with the subsequent response package )
Then is TCP Disconnect operation of module , Refer to the content written before for details , Very detailed . No more details here .
5. The server returns a response message
When the server completes all kinds of processing of the request message , You can return the response message . The working process here is the same as when the client sends a request message to the server .
First ,Web Server calls Socket Library write, Deliver the response message to the protocol stack . At this time , You need to tell the protocol stack who to send this response message to , But we don't need to tell the client directly IP Address and other information , Instead, you just need to give the descriptor of the socket used for communication . All the communication states are stored in the socket , It also includes information about communication objects , So as long as there's a descriptor, it's all right .
Next , The protocol stack splits the data into multiple network packets , Then add the head and send it out . These packages contain the address of the receiving client , They will be forwarded by switches and routers , Through the Internet to the client .
Attached is the full picture of the journey of the whole network package


This concludes the whole book , I just briefly summarize the general content of the book , I also omitted some unnecessary contents . This is really a very good introductory book , After reading it, I feel that I have the whole framework of computer network . Next, you should go deep into some books .
What a long long road! , I will go up and down .
边栏推荐
- Visibilitychange – refresh the page data when the specified tab is visible
- PCL知识点——体素化网格方法对点云进行下采样
- MATLAB中nexttile函数使用
- chrome瀏覽器快速訪問stackoverflow
- HDU - 1114 Piggy-Bank(完全背包)
- OpenHarmony如何启动FA(本地和远程)
- TCP拥塞控制详解 | 2. 背景
- SAP commerce Cloud Architecture Overview
- Sword finger offer 27 Image of binary tree
- Eth data set download and related problems
猜你喜欢

Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)

Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service

Timing / counter of 32 and 51 single chip microcomputer

871. Minimum refueling times

【网络是怎样连接的】第六章 请求到达服务器以及响应给客户端(完结)
![[shutter] dart data type (dynamic data type)](/img/6d/60277377852294c133b94205066e9e.jpg)
[shutter] dart data type (dynamic data type)

每日一题——倒置字符串

ThreadLocal

【GAMES101】作业4 Bézier 曲线

Use of nexttile function in MATLAB
随机推荐
Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture
Alibaba Tianchi SQL learning notes - Day3
traceroute命令讲解
The bottom simulation implementation of vector
easyswoole3.2重启不成功
2022 interview questions
Platform management background and business menu resource management: business permissions and menu resource management design
Si446 usage record (II): generate header files using wds3
Microservice architecture practice: using Jenkins to realize automatic construction
ssb门限_SSB调制「建议收藏」
智能水电表能耗监测云平台
uniapp H5页面调用微信支付
871. Minimum refueling times
将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus
Leetcode question brushing record | 933_ Recent requests
ROS knowledge points -- the difference between ros:: nodehandle N and NH ("~")
How to create a new page for SAP Spartacus storefront
CEPH principle
This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
13、Darknet YOLO3