简体   繁体   English

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

[英]How to set the format of a udp packet?

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. 在分配中,需要通过将2台笔记本电脑中的无线LAN卡切换到ad hoc模式,然后选择合适的SSID,在2台笔记本电脑之间形成ad hoc网络。 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): 然后需要开发一个“发送方”和“接收方”应用程序,以从一台笔记本电脑发送一个简单的“ Hello World”消息,然后使用套接字在另一台笔记本电脑上接收它。我成功地使用了udp数据报套接字和udp数据包(在Java中),因为自组织网络中没有服务器。唯一的问题是,数据包格式应如下所示,其中数据表示为最大长度= 64 KB的字节数组(假定提供了IP地址) (以2个字节为单位):

| | | | | | | | | | | |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(); 我知道在接收方,您可以使用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. 我只是想知道udp数据包的实际格式是什么,以及如何在Java中设置udp数据包的格式。 How does creating a dataGram packet by specifying a buffer byte array, the intelAddress and the port sets the format of the packet? 如何通过指定缓冲区字节数组,intelAddress和端口来创建dataGram数据包,以设置数据包的格式? 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. 提前致谢。

The UDP packet has an implied format. UDP数据包具有隐式格式。 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). http://en.wikipedia.org/wiki/User_Datagram_Protocol Java将处理必​​须存在的8个标头字节(源端口,目标端口,长度,校验和)。 Even if you provide only a single byte of data, the 8 byte header will still be correctly created by Java. 即使仅提供一个字节的数据,Java仍将正确创建8字节的标头。 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. 其他值(IP地址和端口)用于形成报头,或实际上在网上发送数据包。

EDIT: Sorry, replaced 4 byte header with 8. My bad. 编辑:对不起,用8替换了4字节的标头。我不好。 You'd think I could read a protocol spec by now. 您可能认为我现在可以阅读协议规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM