From aa143115d500fdb6c8bd29a2de432fe2a5642af7 Mon Sep 17 00:00:00 2001 From: fly6516 Date: Wed, 8 Jan 2025 14:11:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(main.py):=20=E4=BC=98=E5=8C=96=20UDP?= =?UTF-8?q?=20=E5=A4=B4=E9=83=A8=E6=9E=84=E9=80=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 UDP 头部构造中添加注释,解释长度固定为 8 的原因 - 保留原有功能和逻辑不变,仅增加代码可读性 --- main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 56f3fa3..8d85a17 100644 --- a/main.py +++ b/main.py @@ -126,7 +126,8 @@ def construct_udp(src_port, dest_port): bytes: 构造的UDP报文。 """ # UDP头部格式: 源端口号 (2 字节), 目标端口号 (2 字节), 长度 (2 字节), 校验和 (2 字节) - udp_header = struct.pack('!HHHH', src_port, dest_port, 8, 0) # 长度固定为8,校验和暂时为0 + # 长度固定为8,因为UDP头部长度为8字节,校验和暂时为0,稍后由IP层计算并填充 + udp_header = struct.pack('!HHHH', src_port, dest_port, 8, 0) return udp_header # 构造 TCP 报文(SYN) @@ -251,6 +252,7 @@ def send_packet(): 发送报文。 """ try: + # 获取源IP、目标主机和目标端口 src_ip = ip_combobox.get() dest_host = dest_entry.get() dest_port = port_entry.get() @@ -264,6 +266,7 @@ def send_packet(): if not validate_port(dest_port): raise ValueError("端口号无效,必须是有效的正整数(1-65535)") + # 解析目标IP地址 dest_ip = socket.gethostbyname(dest_host) src_port = 12345 dest_port = int(dest_port)