马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;">UDP 协议简介</strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">UDP是一个简单的面向数据报的传输层协议:进程的每个输出操作都正好产生一个 UDP数据报,并组装成一份待发送的IP数据报。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">这与面向流字符的协议不同,如 TCP,应用程序产生的全体数据与真正发送的单个IP数 据报可能没有什么联系。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">UDP不提供可靠性:它把应用程序传给IP层的数据发送出去,但是并不保证它们能到达目的地。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">应用程序必须关心IP数据报的长度。如果它超过网络的 MTU,那么就要对IP数 据报进行分片。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">UDP/IP 的报文结构如下图所示:</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462721755972966554775.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="84" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462721755972966554775.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462723815375394997058.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="82" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462723815375394997058.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● 端口号表示发送进程和接收进程。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● TCP端口号与UDP端口号是相互独立的。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● 尽管相互独立,如果TCP和UDP同时提供某种知名服务,两个协议通常选择相同的端口号。这纯粹是为了使用方便,而不是协议本身的要求。(DNS)</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● UDP长度字段指的是UDP首部和UDP数据的字节长度。该字段的最小值为 8字节。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462731999630258393789.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="135" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462731999630258393789.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● UDP检验和是覆盖UDP首部和UDP数据。IP首部的校验和,它只覆盖IP的首部</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● UDP的检验和是可选的,而TCP的检验和是必需的。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● IP计算校验和和UDP计算校验和之间存在不同的地方。首先, UDP数据报的长度可以为 奇数字节,但是检验和算法是把若干个 16 bit字相加。解决方法是必要时在最后增加填充字节 0,这只是为了检验和的计算(也就是说,可能增加的填充字节不被传送)。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">● UDP数据报和TCP段都包含一个1 2字节长的伪首部,它是为了计算检验和而设置的。伪首部包含IP首部一些字段。其目的是让UDP两次检查数据是否已经正确到达目的地 (例如,IP没有接收地址不是本主机的数据报,以及 IP没有把应传给另一高层的数据报传给 UDP)。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">上图中,我们特地举了一个奇数长度的数据报例子,因而在计算检验和时需要加上填 充字节。注意,UDP数据报的长度在检验和计算过程中出现两次。 如果检验和的计算结果为 0,则存入的值为全1(65535),这在二进制反码计算中是等效的。如果传送的检验和为0,说明发送端没有计算检验和。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">如果发送端没有计算检验和而接收端检测到检验和有差错,那么 UDP数据报就要被悄悄 地丢弃。不产生任何差错报文(当 IP层检测到IP首部检验和有差错时也这样做)。 UDP检验和是一个端到端的检验和。它由发送端计算,然后由接收端验证。其目的是为了发现UDP首部和数据在发送端到接收端之间发生的任何改动。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; font-size: 14px;">Python 实现UDP协议</span></strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">Python利用Socket模块来实现UDP协议。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;">UDP Socket Server的示例程序:</strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462740038700976332858.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="211" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462740038700976332858.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;">UDP Socket Client的示例程序:</strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462742373476945030913.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="150" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462742373476945030913.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;">运行UDP Socket Client程序并开启抓包</strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462746054182372189489.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="189" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462746054182372189489.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462746716689302798847.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="119" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462746716689302798847.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">从抓取到的数据包可以看到:</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">1、没有会话建立的过程。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">2、数据并没有确认。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">3、应用的每一次操作,网络上对应一个数据包。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">4、服务器端没有开启侦听对应的UDP端口,就会自动发送ICMP的差错报文给发送端。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">对发送UDP报(红色框中的1号)和响应报(红色框中的2号)进行详细分析如下图:</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462749478581592792588.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="126" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462749478581592792588.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;">两种Python 发送UDP数据方式</strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">方式一:Socket发送</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462753739513222974498.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="120" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462753739513222974498.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">方式二:Scapy sr1发送</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><br/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><a target="_blank" href="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462758173254863598445.png" style="padding: 0px; margin: 0px; color: rgb(50, 50, 50); text-decoration-line: none; background-color: transparent; border: none; outline: none !important;"><img width="230" height="70" src="https://www.ad.siemens.com.cn/club/bbs/upload/image/20231103/6383462758173254863598445.png" style="padding: 0px; margin: 0px; border: 0px; vertical-align: middle;"/></a></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);"><strong style="padding: 0px; margin: 0px;">两种方式的差别:</strong></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">方式一是把UDP数据转交给驱动程序处理,数据长度超了MTU后,数据的分片由驱动来自动实现,劣势是不能指定发送的网卡接口。</p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; font-family: "Noto Sans SC", "Microsoft YaHei", ���ź�, -apple-system, BlinkMacSystemFont, "segoe ui", Roboto; color: rgb(51, 51, 51); font-size: 12px; text-wrap: wrap; background-color: rgb(255, 255, 255);">方式二是通过Scapy发送数据,数据不转交驱动程序,所以有分片需求时,不能被自动实现分片功能,优势是可以选择发送的接口网卡。</p><p><br/></p> |