type
status
date
slug
summary
tags
category
icon
password

目前关于UDP的了解

  • 尽力而为的服务
    • 报文段
    • 可能丢失
    • 送到应用进程时乱序
  • 无连接
    • UDP发送端和接收端没有握手
    • 每个UDP报文段都被独立地处理
    • 头部较小,传输效率高
  • 在UDP上实现可靠传输
    • 在应用层增强可靠性
    • 应用特定的差错恢复
  • DNS is an example of an application-layer protocol that typically uses UDP
    • Without performing any handshaking with the UDP entity running on the destination end system, the host-side UDP adds header fields to the message and passes the resulting segment to the network layer
    • The network layer encapsulates the UDP segment into a datagram and sends the datagram to a name server.
    • The DNS application at the querying host then waits for a reply to its query.
    • If it doesn’t receive a reply (possibly because the underlying network lost the query or the reply), it might try resending the query, try sending the query to another name server, or inform the invoking application(调用的应用程序) that it can’t get a reply.
  • 有应用需要UDP的原因
    • Finer application-level control over what data is sent, and when
      • UDP will package the data inside a UDP segment and immediately pass the segment to the network layer
      • 而TCP有拥塞控制和流量控制——导致排队时间长、延迟高
      • TCP will also continue to resend a segment until the receipt of the segment has been acknowledged by the destination, regardless of how long reliable delivery takes.
      • 应用可接受一定的数据丢失等错误
    • No connection establishment
    • No connection state
    • Small packet header overhead(头部开销小)
  • 使用UDP的应用
    • notion image

UDP Segment Structure

notion image
  • The application data occupies the data field of the UDP segment
  • The UDP header has only four fields, each consisting of two bytes
  • the port numbers allow the destination host to pass the application data to the correct process running on the destination end system(that is, to perform the demultiplexing function)
  • The length field specifies the number of bytes in the UDP segment (header plus data)
  • The checksum is used by the receiving host to check whether errors have been introduced into the segment
这里需要辨析一下UDP报文段内容和UDP套接字(socket)的区别
UDP报文段的内容是包含源端口号和目的端口号的(准确一点是报文段的首部内容包含),不包含源IP地址和目的IP地址(这部分是属于网络层而不是传输层),但是,接收方接收到UDP报文,需要定位到对应的进程的时候,是通过一个二元组(目的IP地址,目的端口号)来找到对应的UDP套接字从而找到对应的进程。而这里目的IP地址就不是从UDP报文段里面找到的,而是从网络层的数据报里面找到的。其实同样的TCP报文段也只是包含了源端口和目的端口,并没有包含源IP地址和目的IP地址,然后TCP的套接字是通过四元组(源端口、目的端口,源IP地址、目的IP地址)来定位的,从而找到对应的进程。多的源信息,就是因为TCP是面向连接的,所以需要知道对方,不同的来源会有不同的套接字(对应进程/线程)来处理。

UDP校验和(checksum)

校验和简介和用法

  • 目标
    • 检测在被传输报文段中的差错
      the checksum is used to determine whether bits within the UDP segment have been altered (for example, by noise in the links or while stored in a router) as it moved from source to destination
发送方:
  • 将报文段的内容视为16bit的整数
  • 校验和:报文段的加法和(1的补运算)
  • 发送方将校验和放在UDP的checksum字段
接收方:
  • 计算接收到的报文段的校验和
  • 检查计算出的校验和与校验和字段的内容是否相等
    • 不相等——检测到差错
    • 相等——没检测到差错但不代表没有差错

校验和计算示例

notion image

校验和计算的细节

  • 进位回滚
  • 结果取补码
  • 目标端:校验范围+校验和=1111111111111111通过校验
数组Computer Networking Notes
  • Giscus