如何在一个 UDP 数据包中发送大块数据? [英] How to send big chunk of data in one UDP packet?

查看:60
本文介绍了如何在一个 UDP 数据包中发送大块数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 Netcat 发送 UDP 数据包

nc -u 127.0.0.1 1234

并使用 tcpdump 查看实际数据包

tcpdump -i any -vv -n udp dst 端口 1234

理论上 UDP 数据包大小可以约为 64K,但是当我发送大小大于 2048 的消息时,Netcat 会拆分数据并发送 2 个单独的 UDP 数据包.例如,如果我发送以下长字符串

<预> <代码> 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

tcpdump 将显示 2 个数据包:

08:13:09.171828 IP (tos 0x0, ttl 64, id 25262, offset 0, flags [DF], proto UDP (17), length 2076)127.0.0.1.33279 >127.0.0.1.1234:[坏的udp cksum 0x061c ->0x7eb9!] UDP,长度 204808:13:09.171842 IP (tos 0x0, ttl 64, id 25263, offset 0, flags [DF], proto UDP (17), length 981)127.0.0.1.33279 >127.0.0.1.1234:[错误的 udp cksum 0x01d5 ->0x3723!] UDP,长度 953

是否可以通过 Netcat 或其他工具在一个 UDP 数据包中发送长消息?在 Internet 上找不到 Netcat 的任何选项.

附言我使用的是 Ubuntu 14.04

解决方案

问题是因为 netcat 读取特定大小的块并写入这些块大小.而且由于它是从流(即标准输入)中读取的,因此无论如何都无法定义诸如消息边界之类的东西.我看不到任何使用 nc 更改读取大小的选项,但是如果您需要一些命令行来发送更大的数据包,您可以使用 Perl 或类似的工具:

$ dd if=/dev/zero bs=1024 count=32 |perl -MIO::Socket::INET -e \'IO::Socket::INET->new(PeerAddr => q[127.0.0.1:1234], Proto => q[udp])->send(do { local $/; ;})'$ tcpdump -i lo -n 端口 1234... IP 127.0.0.1.42061 >127.0.0.1.1234:UDP,长度 32768

trying to send UDP packets using Netcat

nc -u 127.0.0.1 1234

And using tcpdump to see actual packets

tcpdump -i any -vv -n udp dst port 1234

In theory the UDP packet size can be about 64K, however when I'm sending a message with size bigger than 2048 the Netcat splits the data and sends in 2 separate UDP packets. For example if I send the following long string

012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

the tcpdump will show 2 packets:

08:13:09.171828 IP (tos 0x0, ttl 64, id 25262, offset 0, flags [DF], proto UDP (17), length 2076)
    127.0.0.1.33279 > 127.0.0.1.1234: [bad udp cksum 0x061c -> 0x7eb9!] UDP, length 2048
08:13:09.171842 IP (tos 0x0, ttl 64, id 25263, offset 0, flags [DF], proto UDP (17), length 981)
    127.0.0.1.33279 > 127.0.0.1.1234: [bad udp cksum 0x01d5 -> 0x3723!] UDP, length 953

Is it possible to send long messages in one UDP packet via Netcat or some other tool? Couldn't find any options for Netcat on internet.

P.S. I'm using Ubuntu 14.04

解决方案

The problem is because netcat reads in chunks of a specific size and writes in these chunk sizes. And since it is reading from a stream (i.e. stdin) there is no way to define something like message boundary anyway. I cannot see any options to change the read size with nc but if you need some command line to send larger packets you could use Perl or similar like this:

$ dd if=/dev/zero bs=1024 count=32 | perl -MIO::Socket::INET -e \
   'IO::Socket::INET->new(PeerAddr => q[127.0.0.1:1234], Proto => q[udp])->send(do { local $/; <STDIN> })'

$ tcpdump -i lo -n port 1234
... IP 127.0.0.1.42061 > 127.0.0.1.1234: UDP, length 32768                                                                                                                       

这篇关于如何在一个 UDP 数据包中发送大块数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆