首页 > 技术文章 > 运输层协议——UDP

biaoJM 2018-05-06 16:39 原文

UDP概述:

UDP只是做了运输层协议能做的最少工作,仅做了复用/分解,少量的差错检验。

UDP是无连接的。

UDP优点:

  • 关于何时、发送什么数据的应用层控制更为精细:TCP在拥堵时会遏制发送方的发送;在发送失败时会持续发送直至成功
  • 无需建立连接:不会有建立连接的延迟
  • 无连接状态:需要在端系统中维护必要的参数
  • 分组头部开销小:8字节,而TCP需要20字节

UDP报文结构:

                  0      7 8     15 16    23 24    31  
                 +--------+--------+--------+--------+ 
                 |     Source      |   Destination   | 
                 |      Port       |      Port       | 
                 +--------+--------+--------+--------+ 
                 |                 |                 | 
                 |     Length      |    Checksum     | 
                 +--------+--------+--------+--------+ 
                 |                                     
                 |          data octets ...            
                 +---------------- ...                 

                      User Datagram Header Format

首部(前两行):由四个字段组成,每个字段两个字节

  1. 源端口号:是可选的字段。当可用时用来指示发送方的端口号,或者再没有其他信息的情况下指向应答端口。当不使用时值为0
  2. 目的端口号:目的端口号
  3. 长度:UDP报文段中的字节数(首部加数据)
  4. 检验和:用于检测报文段中是否出现了差错
  1. Source Port is an optional field, when meaningful, it indicates the port of the sending process, and may be assumed to be the port to which a reply should be addressed in the absence of any other information. If not used, a value of zero is inserted.
  2. Destination Port has a meaning within the context of a particularinternet destination address. 
  3. Length is the length in octets of this user datagram including thisheader and the data. (This means the minimum value of the length iseight.) 
  4. Checksum is the 16-bit one's complement of the one's complement sum of apseudo header of information from the IP header, the UDP header, and thedata, padded with zero octets at the end (if necessary) to make amultiple of two octets.

伪首部(pseudo header):

伪首部并非UDP数据报中实际的有效成分,而是一个虚拟的数据结构,其中的信息是从数据报所在IP分组头的分组头中提取的,既不向下传送也不向上递交,而仅仅是为计算检验和。

伪头结构:

              0      7 8     15 16    23 24    31 
                 +--------+--------+--------+--------+
                 |          source address           |
                 +--------+--------+--------+--------+
                 |        destination address        |
                 +--------+--------+--------+--------+
                 |  zero  |protocol|   UDP length    |
                 +--------+--------+--------+--------+

UDP检验和:

发送方将UDP报文(首部加数据)和伪头的16位比特字的和按位取反作为检验和放到检验和字段中。

求和过程中所有的溢出都要回卷。

接收方将接收到的报文和伪头(包含检验和)重新计算检验和,如果得到的结果为"FFFF"则说明没有出错。之所以是FFFF,是因为接收方的计算加上了发送方计算的检验和的按位反。


UDP仅提供差错检验,并没有恢复功能。但是可以在应用层进行人为添加。

参考来源:

https://datatracker.ietf.org/doc/rfc768/?include_text=1

《计算机网络:自顶向下方法》


推荐阅读