如何设置udp数据包的格式?

发布于 2024-10-12 21:09:45 字数 743 浏览 9 评论 0原文

在一项作业中,需要通过将两台笔记本电脑中的无线网卡切换到 ad hoc 模式,然后选择合适的 SSID,在 2 台笔记本电脑之间形成 ad hoc 网络。 我在实验室之间创建了一个临时网络。然后需要开发一个“发送方”和“接收方”应用程序,以从一台笔记本电脑发送一条简单的“Hello World”消息,并使用套接字在另一台笔记本电脑上接收它。我使用 udp 数据报套接字和 udp 数据包成功完成了这一任务(在 java 中),因为 adhoc 网络中没有服务器,唯一的问题是数据包格式应如下所示,其中数据表示为最大长度 = 64 KB 的字节数组(假设提供了 IP 地址)。在 2 个字节中):

| | | | | | | | | | | |H|E|L|L|O| |W|O|R|L|D|                          
__________|___________| 
Sender ID |Reciever ID|       data
(4 bytes) (4 bytes)   

我知道在接收方您可以使用 packet.getAddress(); 获取发送数据包的地址,并且接收方的地址已经知道,但我不明白这种格式的目的或如何设置数据包的格式。我只是想知道 udp 数据包的实际格式是什么以及如何在 java 中设置 udp 数据包的格式。如何通过指定缓冲区字节数组、intelAddress 和端口来创建 dataGram 数据包。设置数据包的格式?我是否需要将缓冲区字节数组修改为与上述相同的格式(尽管发送者的地址仍然被发送)?请尽快回复我。任何帮助将不胜感激。提前致谢。

In an assignment it was required to form an ad hoc network between 2 laptops by switching the wireless LAN card in 2 laptops to the ad hoc mode then choosing a suitable SSID.
I've created an adhock network between labtops. It was then required to Develop a “Sender” and a “Receiver” application to send a simple “Hello World" message from one laptop and receive it on the other one using sockets. I did it successfully using udp datagram sockets and udp packets (in java) as there is no server in adhoc networks. The only problem is that it was stated that packet format should look like the following, where data is represented as an array of bytes of maximum length = 64 KB (assume IP address is presented in 2 bytes):

| | | | | | | | | | | |H|E|L|L|O| |W|O|R|L|D|                          
__________|___________| 
Sender ID |Reciever ID|       data
(4 bytes) (4 bytes)   

I know that at the reciver side you can get the address of the sent packet using packet.getAddress(); and the address of the reciever side is already known but I dnt understand the purpose of this format or how to set the format of a packet. I just wanna know what is the actual format of a udp packet and how to set the format of udp packet in java. How does creating a dataGram packet by specifying a buffer byte array, the intelAddress and the port sets the format of the packet? Do i need to modify the buffer byte array to be of the same format as stated above (although the address of the sender is sent anyways) ? Please reply to me as soon as you can. Any help will be appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-10-19 21:09:45

UDP 数据包具有隐式格式。 http://en.wikipedia.org/wiki/User_Datagram_Protocol Java 将处理第 8 个标头必须存在的字节(源端口、目标端口、长度、校验和)。即使您只提供一个字节的数据,Java 仍然会正确创建 8 字节标头。您不负责形成这些标头,只负责提供其中的值。

您所能控制的只是数据包的数据部分。可以按照您喜欢的任何方式格式化。听起来您需要将其格式化,如上面所示。该格式的值可能是有争议的(源地址、目标地址),但听起来无论如何您都必须遵守它。

是的,您必须将缓冲区字节数组修改为与您提供的格式相同。其他值(IP 地址和端口)用于形成标头,或实际在线路上发送数据包。

编辑:抱歉,用 8 字节替换了 4 字节标头。我的错。你可能认为我现在可以阅读协议规范了。

The UDP packet has an implied format. http://en.wikipedia.org/wiki/User_Datagram_Protocol Java will take care of the 8 header bytes that must be there (source port, destination port, length, checksum). Even if you provide only a single byte of data, the 8 byte header will still be correctly created by Java. You are not responsible for forming those headers, only in providing the values that go in them.

All you have control of is the Data section of the packet. That can be formatted any way you like. It sounds like you are required to format it as you have shown above. The value of the that format may be debatable (source, destination addresses), but it sounds like you must adhere to it regardless.

Yes, you must modify the buffer byte array to be the same format as you provide. The other values (IP address and Port) are used either for forming the header, or actually sending the packet on the wire.

EDIT: Sorry, replaced 4 byte header with 8. My bad. You'd think I could read a protocol spec by now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文