History visit a web page, network

Transfer from https://onlyangelia.github.io/computerIntnet/intnetlink/

Explaining the connection process, first explain a few, to elaborate pave the way back. When our computer starts, the DHCP protocol (also belonging to the application layer protocol based on UDP protocol, full Dynamic Host Configuration Protocol: Dynamic Host Configuration Protocol) for dynamic configuration of IP addresses (of course, you can manually configure the IP, the average person will not to do so), and the unique MAC address (MAC full name of our computers will have corresponding: Media Access Control, MAC), send a network packet, the destination address is necessary to include IP addresses, but also contains the MAC address. IP addresses like address, MAC address similar to the identity card, both are indispensable. Query: (Address Resolution Protocol ARP, is a protocol belonging to the link layer partial) maps IP addresses and MAC addresses may by APR protocol. Remember the IP address and MAC address of these two concepts.

In the TCP / IP five layer model, through the browser in the browser page, let's sort out the connection process under the network. For example, we enter in the browser: https://onlyAnglia.github.io  , press Enter, showing the blog content through the middle of the process to which the browser? Briefly speaking under HTTP connection, and then add HTTPS connections on the basis of HTTP. Of course, there are many application-layer protocols, such as RTMP, QUIC, GTP and the like, there is based on TCP, UDP-based there. Examples used here HTTP over TCP protocol, used transport layer protocol related to different TCP connections and UDP and detailed connection procedure, explained later write alone, let's look at network connection.

Whole transmission process is as follows:

 

 
TCP-IP five layer model for data transmission .png

First, machines and people do not like, and do not recognize that we can memorize the domain name, the machine only recognize IP addresses,

step1: DNS resolution to obtain the corresponding IP address (DNS resolution is more complex, just say here that under normal procedures, related to load balancing, etc. aside)

The browser looks onlyAnglia.github.io corresponding IP addresses in the local DNS cache, if found the corresponding IP is returned; if the local DNS cache not find the corresponding IP address, the next will be asked like a local DNS server (If it is configured through DHCP, the local DNS server by a corresponding Internet service provider (ISP automatically assigned), if found directly back to IP addresses; if not found, the local DNS server will ask its root domain name servers, root name server told local DNS server which the top-level domain servers to ask, after the top-level domain name server will tell the local DNS server authoritative name server to ask what; the next local DNS server to find the authoritative name server queries corresponding to the corresponding IP address, the authoritative server the corresponding IP address to the local DNS server, the local DNS server then returns the IP address to the browser, the browser receives the corresponding IP address, ready to establish a connection.

step2: using the HTTP protocol, the application layer through the packaging

After the query to the IP address corresponding to the domain name indicates the target is requesting access exist, it can be accessed. Construction HTTP request then the application layer, client's HTTP request packet, called packets, or most used HTTP1.1, the request line included in the request packet HTTP1.1, header, body entity. Wherein the method comprises a user request line request, and the request URI HTTP version number. For example: POST /form/entry HTTP1.1 header field contains a variety of conditions and requests request attributes, but some fields request header field, is the common header field, as well as part of the entity header field (see on this part may be: the HTTP protocol simple explanation ). After building the request packet, the binary stream to stream through the transport layer. And the browser open port to listen.

step3: receiving a request message transport layer, the build request segment (mainly UDP protocol layer protocol and TCP protocol, HTTP protocol is based on TCP)

After application layer request packet is transmitted to the transport layer, the transport layer packet transmission layer encapsulation according to the protocol to be used, here constructed after the TCP header, the transport layer constructed for TCP header, the transmitted stream information as data item. TCP header is more complex, and this reliable TCP connection is related to the transport layer at this time after the kernel will open, listening port number, wait for the next response. (Usually there is a card in the machine, the machine is also equipped with a plurality of portions of the card) In this case the browser is SYN-SENT state.

 
TCP header .jpg

 

step4: receiving network layer transport layer segment constructed IP packet (called floor network transmission frame, called the network layer packet, called a transport layer segment)

After receiving the network layer to the transport layer TCP packets transmitted over the first task is to encapsulate IP packets, the application layer transmitted by a TCP packet as a data item, adding IP header, the IP packet is formed. The transport layer protocol header TCP header includes a source port number, destination port number, IP network layer in the Source Address, the destination address in the IP packet packaged. In addition, IP packet also includes versions, header length, type of service TOS, total length, logos, flags, fragment offset, header checksum, the TTL and so on. After the completion of the network layer is encapsulated IP packet, the IP packet transmitted to the link layer.

step5: data link layer receives the IP packet, the MAC frame Construction

数据链路层又可称为MAC层(MAC全称 Media Access Control 媒体访问控制),MAC层接收到IP包后,开始构建MAC帧,MAC帧开始是目标MAC地址和源MAC地址,源MAC地址毫无疑问是我们本机的MAC地址,本机的MAC地址在该设备被创造出来的时候就有,并且是唯一的(要想查看IP地址和MAC地址,linux上使用ifconfig或者ip addr,会在终端输出电脑的网络相关信息)。知道了自己设备上的MAC地址,但不知道目标IP地址对应的MAC地址,所以需要一个能够查询IP地址和MAC地址对应的协议,就是ARP(Address Resolution Protocol)协议。ARP协议只是针对IPv4,若是IPv6,要使用NDP协议。机器本地是有ARP协议缓存的,若能在ARP协议缓存中找到IP地址对应的MAC地址,便不会再去请求ARP协议,若没有则会请求ARP协议。在知道了目标IP地址对应的MAC地址后,将目标MAC地址放在MAC帧里,以太网的第二层最后面是CRC,也就是循环冗余检测,(MAC帧的其它组成不在细说)。

step6:MAC帧头构建好后,网关准备发包

MAC帧构建好,在网络中传输的网络包即构建好,然后将网络包发出去。在发包之前IP地址是否在同一个网段内的问题,通过CIDR和子网掩码计算是否在一个网段内,若在同一个网段内直接发出。一般我们访问的网站是不太可能和我们在同一网段内的,那么需要将包发往默认网关,默认网关收入包。

step7:网关查询路由表,通过路由协议进行网络传输

网关收入包后,取下MAC帧和IP包,判断该网哪里转发,根据路由算法,选择一个合适的网段。网段的选择会涉及到两种形式的路由算法,一种是静态路由,一种是动态路由。静态路由就是在路由器上配置一条条规则,维护路由表。可以通过route命令和ip route命令查看进行静态路由的查询和配置。 动态路由使用动态路由路由器,根据路由协议算法生成动态路由表,随网络运行情况的变化而变化。动态路由协议算法也涉及两大类,第一大类算法称为距离矢量路由(distance vector routing)适用于小型网络,最早的路由协议RIP采用该算法,第二大类算法是链路状态路由(link state routing)。内部网关协议采用基于链路状态路由算法的OSPF(Open Shortest Path First,开放式最短路径优先),网络的路由协议是基于距离矢量路由算法的BGP(Border Gateway Protocol)。BGP分为两类,eBGP和iBGP。对于网络包,每个数据中心有自己的规则,网络中这种不同规则所构成的网络包为自治系统AS(Autonomous System)。自治系统间边界路由器使用eBGP广播路由,内部运行iBGP,让内部路由器快速找到到达外网目的的最好的边界路由器。路由器之间信息交换使用的协议,RIP使用UDP协议,OSPF直接发送IP包,而BGP使用的是TCP协议,路由之间会建立TCP连接,每60s发送一次keep-alive 消息。此外HTTP 1.1默认keep-alive是开启的。这样网络包就像跳方格一样跳了多个(也许是一个)路由器之后,终于找到了目的IP地址所在的网关。

step8:找到目的IP地址后,网关取下MAC头,将IP包发送给目的主机的网络层,检查IP地址是否对上

目的IP地址所在的网关接收到网络包后,发现在同一个网段内, 将包收入,然后发送给目的主机,目的主机取下MAC头,判断一下MAC地址和自己的相符,然后将IP包传给网络层,网络层取下IP包头,查看IP地址和自己的IP地址是否对上。

step9:IP地址对上之后,网络层将包传递给传输层,TCP发确认包,会延刚才的方向报平安,直到收到平安到达的回复,进行TCP握手

网络层数判断IP地址对上之后,根据IP头中的协议项,知道自己上层还需要TCP协议,将包传递给传输层,目的主机的内核开启,当目的主机有了IP的端口号,就可以调用listen函数进行监听(TCP和UDP都是基于 Socket, 而 listen函数 是Socket里的函数,有关 Socket 这里不详细解说),目的主机处于LISTEN状态。传输层在收到TCP请求段后,会发送ACK包确认到达,此时目的主机处于SYN-RCVD状态,发送的网络包延刚才的路径传回,浏览器在接收到目的主机发送的SYN,ACK后进入ESTABLISHED状态,同时再发出ACK,当目的主机接收到ACK后也进入ESTABLISHED状态,此时TCP三次握手完成。

step10:TCP收到回复后,进行目的端口匹配,将包内容传给HTTP服务,RPC统筹处理请求,告诉相关进程

TCP三次握手完成后,进行目的端口匹配,之后将包内容传给上层的HTTP服务,RPC统筹处理,告诉相关进程(Soket可能是进程在维护也可能是线程在维护监听,并且TCP往往会创立两个Socket,一个叫做就监听Socket,一个是已连接Socket,这里不详细讲述)。

step11:RPC处理完毕,会回复一个HTTP/HTTPS包告知操作成功,准备向接收端传输处理结果

在RPC处理完毕后,目的主机开始向接收方发送数据。在连接建立的时候,两方已商定起始ID,但数据的发送不是一个发送完等回复后再发送另一个,而是通过累计确认或者叫累计应答(cumulative acknowledgment) 的模式进行传输,即在应答某个之前的ID表示都收到了。这样TCP需要双方有缓存进行记录,发送方的记录分四部分:第一部分是发送了并且已确认,第二部分是发送了并且尚未确认,第三部分是没有发送但在等待发送,第四部分是没有发送暂时也不会发送。第一部分和第二部分的分界线是lastByteAcked,第二部分和第三部分的分界线是lastByteSent,整个第二部分和第三部分的和 Advertised window 。 浏览器作为接收端在TCP报文里是会告诉发送端这个窗口大小的,超过了这个窗口的包,接收端无法接收就会丢弃。(有关接收方的窗口说明详情可自行查询)

step 12: TCP慢启动,之后开始进行数据传输,并随时监测窗口大小进行流量控制和拥塞控制

虽然浏览器作为接收方告诉了发送方窗口大小,但网络是瞬息万变的,也许这会网络变差了或者网段了,那么TCP在一开始的时候,为了避免造成通道容量溢出,一条TCP连接后,设置只能发送一个报文段cwnd(congestion window 拥塞窗口)设置为1,在收到一个确认后,cwnd加一,一次能够发送两个,当这两个报文段的确认到来的时候,每个cwnd都加一,这样两个cwnd就加二,一次就能发四个,开始呈现指数级增长。当一次发送超过ssthresh的值时代表快要溢出,此时cwnd改为增加1/cwnd,一轮下来增长一个......这是有关传输过程中的拥塞控制,关于拥塞控制仍有一些问题存在,具体的流程可自查询相关的流量控制和拥塞控制。

step 13:接收方浏览器接收到数据后,开始进行处理,逐渐在浏览器上显示

通过数据的传输,接收端接收到了来自发送方的网络包,当接收到一个网络包时,TCP最终将包传递给浏览器,让浏览器处理HTTP应答报文,这样随着应答报文的数量增加,网页中的内容也会逐渐的显示在浏览器上。

step 14: 数据传输完成,进行连接断开,四次挥手说再见

数据终于传输完成,到了该断开的时候。但我们知道TCP是可靠的传输连接,是相对靠谱的,那作为靠谱的协议在断开的时候不能说断开就断开,并且TCP是全双工的,所以接收端和发送端需要各自断开。数据传输完毕时,服务器和浏览器作为发送方和接收方都处于ESTABLISHED状态,此时服务器知道数据已传输完毕,准备断开,就向接收方发送FIN报文,进入FIN-WAIT-1状态;接收方收到FIN报文后,向发送方发送ACK,进入CLOSED-WAIT状态;服务端收到ACK后从FIN-WAIT-1状态进入FIN-WAIT-2状态;接收方进入CLOSED-WAIT状态结束后再向发送方发送FIN,ACK ,并进入LAST-ACK状态;发送方接收到FIN、ACK后从FIN-WAIT-2状态进入TIME-WAIT状态(等待2MSL),并且发送ACK;接收方接收到ACK后进入CLOSED状态;服务端等待2MSL(MSL: Maximum Segment Lifetime ,报文最大生成时间)后也进入CLOSED状态。至此,建立的连接已经各自断开,整个连接过程结束。

为了更好的理解TCP三次握手和四次挥手,附上TCP状态机 

 
TCP状态机.jpg

 

以HTTP为例讲述了网络的连接过程,其它协议与此大同小异,若是基于UDP的协议会在上述步骤中简化很多,数据传输中的socket维护也相对简单。以上就是网络的连接过程。

Guess you like

Origin www.cnblogs.com/tymagic/p/11533517.html