计算机网络系列(3)之传输层TCP and UDP

来源:互联网 发布:itunes下载安装软件 编辑:程序博客网 时间:2024/06/05 06:57

1. TCP
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议。

功能1:通过数据校验和,出错重传,超时重传等机制,保障数据的可靠性传输;功能2:流量控制,通过滑动窗口协议,避免数据接收方的缓冲区不够(数据传输大小),发送方一次性发送太多数据,导致数据溢出overflow;功能3:拥塞控制,当网络出现拥塞,迅速降低(数据传输速率),直到数据传输正常,然后缓慢增大数据传输速率。

2. TCP package format
TCP数据包由TCP header和TCP data构成。其中,TCP header 的构成如下:Source port number + destination port number + sequence number + acknowledge number + HLEN + Reserved + URG/ACK/PSH/RST/SYN/FIN + Window size + Checksum + Urgent pointer

这里写图片描述

2.1. Sort number
用于识别主机中的某一个程序,每一个端口号唯一对应一个程序,如果两个程序尝试去使用同一个端口号,会出现端口被占用的错误。

2.2. Sequence number & Acknowledge number
客户端和服务器在通过TCP协议交互时,双方分别维护一个序列号。序列号哦目的是默认收到该序列号之前的所有数据包,以及,请求该序列号的数据包。配合

2.3. Window size
这个包的发送方告诉连接的另一端: 他的TCP size value是276个字节. 你不要一次性发送超过276bytes的数据给我,否则我会缓冲区溢出overflow。

3. Three-way handshaking
3.1. Three-way handshaking
Get approval from the other party + Initialize communication (e.g., exchange IP address, initialize sequence number and etc). TCP三次握手的目的主要有两个:其一是经过双方的确认,建立一个TCP连接;其二是初始化TCP连接的相关信息,比如序列号,比如绑定IP地址,等。

这里写图片描述

4. TCP functions
结合TCP的数据包组成结构,讲讲TCP的功能。
4.1. Reliable data delivery
4.1.1. Re-sequencing
TCP re-sequences the received TCP segments into the correct order based on the sequence number and the segment size.

4.1.2. Error control
When data is transmitted, communication noise or distortion may cause errors.

4.1.3. Error detection
TCP checksum: TCP header + TCP data + Psudo-header, where the psudo-header is used to identify the mis-delivered segments.

4.1.4. Retransmission
When a destination receives an erroneous segment or detect a lost segment, it requests the source to retransmit erroneous or lost segment.

4.1.5. Timeout Interval
TCP measures the mean round trip and the deviation of round trip time. The timeout interval is selected to be timeout = mean + 4*deviation.

4.2. Flow control
4.2.1. Meanings
Control the flow of data from the source to the destination such that there is no buffer overflow at the destination.

4.2.2. Sliding window protocol
Let the destination have a buffer size of W bytes. At any time, the source can send at most W bytes, even if it has not received their acknowledgments.

4.3. Congestion control
4.3.1. Meaning
When the Internet has heavy traffic, there is congestion. 参见TCP数据包之RTT/RTO测量分析(http://blog.csdn.net/zhangskd/article/details/7196707)。

4.3.2. Ideas
When the source experiments a timeout, there are two possibilities: a. there is congestion; b. the segment/acknowledgment has been lost.

When there is congestion, the source quickly reduces the transmission rate; When there is no congestion, the source slowly increases the transmission rate until reaching the original one.

5. UDP
5.1. Meaning
UDP is another transport protocol executed at the communication endpoints. It is connectionless, but not guarantee reliable data delivery and not execute flow and congestion control.

6. Questions
Q1: What is the purpose of port numbers?
A port number is a way to identify a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.

Q2: What functions does TCP provide?
reliability transmission (error detection, re-transmission)
flow control
congestion control

Q3: Consider TCP checksum
Case 1: State the error patterns that the TCP checksum cannot detect.
数据全部改变 (e.g., 1,2,3,-6 变成 5,5,5,-15)
位置改变 (e.g., 1,2,3,-6 变成 1,3,2,-6)
一增一减 (e.g., 1,2,3,-6 变成 2,1,3,-6)

Case 2: In a TCP segment, the first and the second bytes of data in a TCP segment are erroneous. Analyze whether the TCP checksum can detect these errors.

这里写图片描述

Checksum = X1+X2+X3+…+Xn
一个X为两字节,因此,前两个字节变化了,一定能检测出来。

6. 总结
重点1:TCP数据包的构成,TCP数据包中有两个重要的字段就是源端口和目标端口,这端口的含义有承上启下的味道。具体地瞎说,对本地的应用程序而言,不同的应用程序绑唯一的本地端口,当TCP解析到相应的端口号的时候,TCP的传输层就知道上一层,即应用层,的某个应用的数据包了,这就是承上;所谓的启下是相对发送方讲的,发送方的某个应用层协议通过操作系统开启绑定某个端口之后,将该端口号告诉传输层,传输层通过该端口号传输来自应用层的信息,这就是启下。

重点2:TCP的功能。主要是数据可靠性的传输,流控,拥塞控制三大要点。具体地将,可靠性传输主要是通过checksum等校验机制完成的,流控的目的是避免数据发送方一下子发送大量的数据,导致接收方一口气吃不成胖子,太勉强,容易出现肠胃病,甚至胃破裂(天,博主有血晕),也就是overflow. 我们可以通过滑动窗口协议来解决,这个协议主要说了一句话:我是讲原则的,每一句对话(each session)别TM超过15个字(这个值可以商量),否则后果自负。

这样做的好处就是每一次最多只能发送这么多的数据量,哪怕对方没有回应!!!直到timeout!!!

还有就是拥塞控制了,如果发送出去的TCP数据报很长一段时间之后才得到回应,或者说TCP数据报的丢包率很严重,这些现象通常我们默认当前网络出现了一个问题:网络拥塞。如何解决这个问题,就是迅速地降低发送TCP数据包的速率,比如,之前是每秒钟1024字节地发送给目标IP,现在出现网络拥塞问题了,我们就迅速将将发送速率减半改为512字节每秒,如果网络问题还是没有缓解,我们就再迅速地减半为256字节每秒,128字节每秒,64字节美妙……2字节每秒,1字节每秒。如果这时候网络拥塞问题缓解了,我们就慢慢地将1字节每秒增加到1.1字节每秒,1.2字节每秒,1.3字节每秒,依次慢慢地增加。这就是TCP拥塞控制的这种迅速降低,缓慢增加的机制。

重点3:UDP。UDP的特点就是不需要建立connection,没有可靠性传输,没有流控,没有拥塞控制。当然,人家UDP有他好的应用场景,比如小规模简单网络管理,比如视频信息传输,比如网络heartbeat。这些场景是以网络数据量取胜的,质可能不显得那么重要——当然我承认这样说是有失偏颇的。

原创粉丝点击