About TCP / IP, these ten questions you all know, a beginning!

Ten Questions About TCP / IP, will know will be of

This article compiled some top ten questions will need to know will be the TCP / IP protocol suite, both high-frequency interview question, but also a necessary foundation programmer literacy.

 
TCP / IP Ten Questions

A, TCP / IP model

TCP / IP protocol model (Transmission Control Protocol / Internet Protocol), consists of a series of network protocols constitute the foundation of the Internet is the Internet's core protocols.

TCP / IP is the protocol reference model is divided into four levels, which are the link layer, network layer, transport layer and application layer. The figure below shows the relationship between the layers of the control TCP / IP model with the OSI model.


 
 

TCP / IP protocol suite hierarchically from top to bottom, the layers of packaging. The top is the application layer, and there are http, ftp, and so we are familiar with the agreement. The second layer is the transport layer, the well-known TCP and UDP protocols at this level. The third layer is the network layer, IP protocol here, which is responsible for the data by the IP address and other data to determine the target transmission. The fourth layer is the data link layer, this level is added to data to be transmitted to an Ethernet protocol header, and CRC coding, in preparation for final data transmission.


 
 

The figure clearly shows the effect of TCP / IP protocol of each layer, and the process of TCP / IP communications protocol in fact corresponds to the data stack and the process stack. Stack process, the data sender encapsulation header portion of each layer continue to the tail, the transmission information is added, to ensure the transmission destination. The stack process, data receiver constantly each header and tail removal, to give a final data transmission.
 
 

In the HTTP protocol as an example in FIG specifically described.

Second, the data link layer

The physical layer is responsible for the physical devices 0,1 bitstream voltage level, exchange between the flash light off. The data link layer is responsible for data sequence into frames 0,1 from a transmission node to other neighboring nodes that are uniquely identified by a MAC (MAC, physical address, a host will have a MAC address).


 
 
  • Encapsulated in a frame: the network layer data packet plus header and tail, encapsulated in a frame, the frame header includes a source MAC address and destination MAC address.
  • Transparent transmission: zero bit stuffing, escape character.
  • Reliable Transfer: rarely used in the link error rate is very low, but the wireless link WLAN will ensure reliable transmission.
  • 差错检测(CRC):接收者检测错误,如果发现差错,丢弃该帧。

三、网络层

1.IP协议

IP协议是TCP/IP协议的核心,所有的TCP,UDP,IMCP,IGMP的数据都以IP数据格式传输。要注意的是,IP不是可靠的协议,这是说,IP协议没有提供一种数据未传达以后的处理机制,这被认为是上层协议:TCP或UDP要做的事情。

1.1 IP地址

在数据链路层中我们一般通过MAC地址来识别不同的节点,而在IP层我们也要有一个类似的地址标识,这就是IP地址。

32位IP地址分为网络位和地址位,这样做可以减少路由器中路由表记录的数目,有了网络地址,就可以限定拥有相同网络地址的终端都在同一个范围内,那么路由表只需要维护一条这个网络地址的方向,就可以找到相应的这些终端了。

A类IP地址: 0.0.0.0~127.255.255.255
B类IP地址:128.0.0.0~191.255.255.255
C类IP地址:192.0.0.0~239.255.255.255

1.2 IP协议头
 
 

这里只介绍:八位的TTL字段。这个字段规定该数据包在穿过多少个路由之后才会被抛弃。某个IP数据包每穿过一个路由器,该数据包的TTL数值就会减少1,当该数据包的TTL成为零,它就会被自动抛弃。
这个字段的最大值也就是255,也就是说一个协议包也就在路由器里面穿行255次就会被抛弃了,根据系统的不同,这个数字也不一样,一般是32或者是64。

2.ARP及RARP协议

ARP 是根据IP地址获取MAC地址的一种协议。

ARP(地址解析)协议是一种解析协议,本来主机是完全不知道这个IP对应的是哪个主机的哪个接口,当主机要发送一个IP包的时候,会首先查一下自己的ARP高速缓存(就是一个IP-MAC地址对应表缓存)。

如果查询的IP-MAC值对不存在,那么主机就向网络发送一个ARP协议广播包,这个广播包里面就有待查询的IP地址,而直接收到这份广播的包的所有主机都会查询自己的IP地址,如果收到广播包的某一个主机发现自己符合条件,那么就准备好一个包含自己的MAC地址的ARP包传送给发送ARP广播的主机。

而广播主机拿到ARP包后会更新自己的ARP缓存(就是存放IP-MAC对应表的地方)。发送广播的主机就会用新的ARP缓存数据准备好数据链路层的的数据包发送工作。

RARP协议的工作与此相反,不做赘述。

3. ICMP协议

IP协议并不是一个可靠的协议,它不保证数据被送达,那么,自然的,保证数据送达的工作应该由其他的模块来完成。其中一个重要的模块就是ICMP(网络控制报文)协议。ICMP不是高层协议,而是IP层的协议。

当传送IP数据包发生错误。比如主机不可达,路由不可达等等,ICMP协议将会把错误信息封包,然后传送回给主机。给主机一个处理错误的机会,这 也就是为什么说建立在IP层以上的协议是可能做到安全的原因。

四、ping

ping可以说是ICMP的最著名的应用,是TCP/IP协议的一部分。利用“ping”命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障。

例如:当我们某一个网站上不去的时候。通常会ping一下这个网站。ping会回显出一些有用的信息。一般的信息如下:


 
 

ping这个单词源自声纳定位,而这个程序的作用也确实如此,它利用ICMP协议包来侦测另一个主机是否可达。原理是用类型码为0的ICMP发请 求,受到请求的主机则用类型码为8的ICMP回应。

ping程序来计算间隔时间,并计算有多少个包被送达。用户就可以判断网络大致的情况。我们可以看到, ping给出来了传送的时间和TTL的数据。

五、Traceroute

Traceroute是用来侦测主机到目的主机之间所经路由情况的重要工具,也是最便利的工具。

Traceroute的原理是非常非常的有意思,它收到到目的主机的IP后,首先给目的主机发送一个TTL=1的UDP数据包,而经过的第一个路由器收到这个数据包以后,就自动把TTL减1,而TTL变为0以后,路由器就把这个包给抛弃了,并同时产生 一个主机不可达的ICMP数据报给主机。主机收到这个数据报以后再发一个TTL=2的UDP数据报给目的主机,然后刺激第二个路由器给主机发ICMP数据 报。如此往复直到到达目的主机。这样,traceroute就拿到了所有的路由器IP。


 
 

六、TCP/UDP

TCP/UDP都是是传输层协议,但是两者具有不同的特性,同时也具有不同的应用场景,下面以图表的形式对比分析。


 
 
面向报文

面向报文的传输方式是应用层交给UDP多长的报文,UDP就照样发送,即一次发送一个报文。因此,应用程序必须选择合适大小的报文。若报文太长,则IP层需要分片,降低效率。若太短,会是IP太小。

面向字节流

面向字节流的话,虽然应用程序和TCP的交互是一次一个数据块(大小不等),但TCP把应用程序看成是一连串的无结构的字节流。TCP有一个缓冲,当应用程序传送的数据块太长,TCP就可以把它划分短一些再传送。

关于拥塞控制,流量控制,是TCP的重点,后面讲解。

TCP和UDP协议的一些应用


 
 

什么时候应该使用TCP?

当对网络通讯质量有要求的时候,比如:整个数据要准确无误的传递给对方,这往往用于一些要求可靠的应用,比如HTTP、HTTPS、FTP等传输文件的协议,POP、SMTP等邮件传输的协议。

什么时候应该使用UDP?

当对网络通讯质量要求不高的时候,要求网络通讯速度能尽量的快,这时就可以使用UDP。

七、DNS

DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析)。DNS协议运行在UDP协议之上,使用端口号53。

八、TCP连接的建立与终止

1.三次握手

TCP是面向连接的,无论哪一方向另一方发送数据之前,都必须先在双方之间建立一条连接。在TCP/IP协议中,TCP协议提供可靠的连接服务,连接是通过三次握手进行初始化的。三次握手的目的是同步连接双方的序列号和确认号并交换 TCP窗口大小信息。


 
 

第一次握手: 建立连接。客户端发送连接请求报文段,将SYN位置为1,Sequence Number为x;然后,客户端进入SYN_SEND状态,等待服务器的确认;

第二次握手: 服务器收到SYN报文段。服务器收到客户端的SYN报文段,需要对这个SYN报文段进行确认,设置Acknowledgment Number为x+1(Sequence Number+1);同时,自己自己还要发送SYN请求信息,将SYN位置为1,Sequence Number为y;服务器端将上述所有信息放到一个报文段(即SYN+ACK报文段)中,一并发送给客户端,此时服务器进入SYN_RECV状态;

第三次握手: 客户端收到服务器的SYN+ACK报文段。然后将Acknowledgment Number设置为y+1,向服务器发送ACK报文段,这个报文段发送完毕以后,客户端和服务器端都进入ESTABLISHED状态,完成TCP三次握手。

为什么要三次握手?

为了防止已失效的连接请求报文段突然又传送到了服务端,因而产生错误。

Specific examples of: generating "expired connection request packet block" in such a case: a first client sends a connection request message segment is not lost, but is located in a network node of a long residence , resulting in delayed to some time after the connection is released before reaching the server. This in itself is already a failure of the segment. But the server receives this request after the failure of the connection segment, it is mistaken for a new connection request issued by the client again. So it is a confirmation message segments to client, agreed to establish a connection. Do not assume that a "three-way handshake", as long as the server a confirmation, a new connection is established. Now that no client requesting establishment of a connection, and therefore will not ignore the acknowledgment server, it will not send data to the server. But the server thought that the new transport connection has been established, and has been sent to wait for client data. In this way, a lot of resources on the server wasted. A "three-way handshake" approach can prevent this phenomenon. For example, that sort of situation, client does not send a confirmation to confirm the server. Since the server does not receive confirmation, we know that client is not required to establish a connection. "

2. Four waving

When the client and server via TCP three-way handshake to establish a connection later, when the data transfer is complete, be sure to disconnect the TCP connection ah. That disconnect for TCP, where there is a mysterious "four times to break up."

 
 

The first break:  Host 1 (enables the client may be a server), set Sequence Number, a FIN segment sent to the host 2; in this case, the host computer 1 enters a state FIN_WAIT_1; this represents no data to the host 1 the host computer 2 to send;

 

Second break:  Host 2 Host received a FIN segment sent 1, 1 to the host back an ACK segment, Acknowledgment Number as Sequence Number plus 1; 1 enters FIN_WAIT_2 host state; Host 2 Host 1 telling, I " agreed to "shut your request;

Third break:  the host computer 2 transmits a FIN segment to the host a request to close the connection, while the host LAST_ACK enters state 2;

第四次分手: 主机1收到主机2发送的FIN报文段,向主机2发送ACK报文段,然后主机1进入TIME_WAIT状态;主机2收到主机1的ACK报文段以后,就关闭连接;此时,主机1等待2MSL后依然没有收到回复,则证明Server端已正常关闭,那好,主机1也可以关闭连接了。

为什么要四次分手?

TCP协议是一种面向连接的、可靠的、基于字节流的运输层通信协议。TCP是全双工模式,这就意味着,当主机1发出FIN报文段时,只是表示主机1已经没有数据要发送了,主机1告诉主机2,它的数据已经全部发送完毕了;但是,这个时候主机1还是可以接受来自主机2的数据;当主机2返回ACK报文段时,表示它已经知道主机1没有数据发送了,但是主机2还是可以发送数据到主机1的;当主机2也发送了FIN报文段时,这个时候就表示主机2也没有数据要发送了,就会告诉主机1,我也没有数据要发送了,之后彼此就会愉快的中断这次TCP连接。

为什么要等待2MSL?

MSL:报文段最大生存时间,它是任何报文段被丢弃前在网络内的最长时间。
原因有二:

  • 保证TCP协议的全双工连接能够可靠关闭
  • 保证这次连接的重复数据段从网络中消失

第一点:如果主机1直接CLOSED了,那么由于IP协议的不可靠性或者是其它网络原因,导致主机2没有收到主机1最后回复的ACK。那么主机2就会在超时之后继续发送FIN,此时由于主机1已经CLOSED了,就找不到与重发的FIN对应的连接。所以,主机1不是直接进入CLOSED,而是要保持TIME_WAIT,当再次收到FIN的时候,能够保证对方收到ACK,最后正确的关闭连接。

第二点:如果主机1直接CLOSED,然后又再向主机2发起一个新连接,我们不能保证这个新连接与刚关闭的连接的端口号是不同的。也就是说有可能新连接和老连接的端口号是相同的。一般来说不会发生什么问题,但是还是有特殊情况出现:假设新连接和已经关闭的老连接端口号是一样的,如果前一次连接的某些数据仍然滞留在网络中,这些延迟数据在建立新连接之后才到达主机2,由于新连接和老连接的端口号是一样的,TCP协议就认为那个延迟的数据是属于新连接的,这样就和真正的新连接的数据包发生混淆了。所以TCP连接还要在TIME_WAIT状态等待2倍MSL,这样可以保证本次连接的所有数据都从网络中消失。

九、TCP流量控制

如果发送方把数据发送得过快,接收方可能会来不及接收,这就会造成数据的丢失。所谓流量控制就是让发送方的发送速率不要太快,要让接收方来得及接收。

利用滑动窗口机制可以很方便地在TCP连接上实现对发送方的流量控制。

设A向B发送数据。在连接建立时,B告诉了A:“我的接收窗口是 rwnd = 400 ”(这里的 rwnd 表示 receiver window) 。因此,发送方的发送窗口不能超过接收方给出的接收窗口的数值。请注意,TCP的窗口单位是字节,不是报文段。假设每一个报文段为100字节长,而数据报文段序号的初始值设为1。大写ACK表示首部中的确认位ACK,小写ack表示确认字段的值ack。


 
 

从图中可以看出,B进行了三次流量控制。第一次把窗口减少到 rwnd = 300 ,第二次又减到了 rwnd = 100 ,最后减到 rwnd = 0 ,即不允许发送方再发送数据了。这种使发送方暂停发送的状态将持续到主机B重新发出一个新的窗口值为止。B向A发送的三个报文段都设置了 ACK = 1 ,只有在ACK=1时确认号字段才有意义。

For each TCP connection has a duration timer (persistence timer). As long as the other party TCP connection to receive notice of zero window, the timer will start continued. If the time duration set by the timer expires, sends a zero window forecasting control segment (carrying a byte of data), then the party receiving the segment on duration timer to reset.

Ten, TCP congestion control

1. Slow start and congestion avoidance

The sender maintains a congestion window cwnd (congestion window) state variable. Congestion window size depends on the degree of network congestion, and dynamically changing. The sender sends the window to make their equal congestion window.

Sender congestion window control principles are: As long as the network is not congested, the congestion window is then somewhat increased, in order to send out more packets. But as long as the network is congested, the congestion window is reduced number, in order to reduce the number of packets injected into the network.

Slow start algorithm:

When the host begins to transmit data, if the immediately poured into a large amount of data bytes of the network, then it may cause network congestion, because the load condition of the network is not clear.
Therefore, a better approach is to conduct surveys, namely small to large gradually increasing the transmission window, that is, from small to big gradually increasing congestion window value.

Usually in the beginning send a message segment, first congestion window cwnd is set to a maximum reported value segment of MSS. And receive a confirmation after each segment of the new, increased up to the MSS a value of the congestion window. In this way gradually increase the congestion window cwnd sender, the packet can be injected into the network is more reasonable rate.

 
 

After each round a transmission congestion window cwnd will be doubled. A transmission round the elapsed time is actually the round trip time RTT. However, "transmission rounds" greater emphasis on: the congestion window cwnd allowed to send the segment are continuously sent and received confirmation of the last byte has been sent.

 

另,慢开始的“慢”并不是指cwnd的增长速率慢,而是指在TCP开始发送报文段时先设置cwnd=1,使得发送方在开始时只发送一个报文段(目的是试探一下网络的拥塞情况),然后再逐渐增大cwnd。

为了防止拥塞窗口cwnd增长过大引起网络拥塞,还需要设置一个慢开始门限ssthresh状态变量。慢开始门限ssthresh的用法如下:

  • 当 cwnd < ssthresh 时,使用上述的慢开始算法。
  • 当 cwnd > ssthresh 时,停止使用慢开始算法而改用拥塞避免算法。
  • 当 cwnd = ssthresh 时,既可使用慢开始算法,也可使用拥塞控制避免算法。
拥塞避免

让拥塞窗口cwnd缓慢地增大,即每经过一个往返时间RTT就把发送方的拥塞窗口cwnd加1,而不是加倍。这样拥塞窗口cwnd按线性规律缓慢增长,比慢开始算法的拥塞窗口增长速率缓慢得多。

 
 

 

无论在慢开始阶段还是在拥塞避免阶段,只要发送方判断网络出现拥塞(其根据就是没有收到确认),就要把慢开始门限ssthresh设置为出现拥塞时的发送 方窗口值的一半(但不能小于2)。然后把拥塞窗口cwnd重新设置为1,执行慢开始算法。

这样做的目的就是要迅速减少主机发送到网络中的分组数,使得发生 拥塞的路由器有足够时间把队列中积压的分组处理完毕

如下图,用具体数值说明了上述拥塞控制的过程。现在发送窗口的大小和拥塞窗口一样大。


 
 

2.快重传和快恢复

快重传

快重传算法首先要求接收方每收到一个失序的报文段后就立即发出重复确认(为的是使发送方及早知道有报文段没有到达对方)而不要等到自己发送数据时才进行捎带确认。


 
 

接收方收到了M1和M2后都分别发出了确认。现在假定接收方没有收到M3但接着收到了M4。

显然,接收方不能确认M4,因为M4是收到的失序报文段。根据 可靠传输原理,接收方可以什么都不做,也可以在适当时机发送一次对M2的确认。

但按照快重传算法的规定,接收方应及时发送对M2的重复确认,这样做可以让 发送方及早知道报文段M3没有到达接收方。发送方接着发送了M5和M6。接收方收到这两个报文后,也还要再次发出对M2的重复确认。这样,发送方共收到了 接收方的四个对M2的确认,其中后三个都是重复确认。

快重传算法还规定,发送方只要一连收到三个重复确认就应当立即重传对方尚未收到的报文段M3,而不必 继续等待M3设置的重传计时器到期。

由于发送方尽早重传未被确认的报文段,因此采用快重传后可以使整个网络吞吐量提高约20%。

快恢复

与快重传配合使用的还有快恢复算法,其过程有以下两个要点:

  • 当发送方连续收到三个重复确认,就执行“乘法减小”算法,把慢开始门限ssthresh减半。
  • 与慢开始不同之处是现在不执行慢开始算法(即拥塞窗口cwnd现在不设置为1),而是把cwnd值设置为 慢开始门限ssthresh减半后的数值,然后开始执行拥塞避免算法(“加法增大”),使拥塞窗口缓慢地线性增大。


     
     

Guess you like

Origin www.cnblogs.com/zhangyu317/p/11225786.html