简体   繁体   English

UDP 数据报套接字中的校验和 + java

[英]Checksum in UDP Datagram Socket + java

It is my understanding that the UDP protocol does not define the action to be taken if the data gets corrupted ie the checksum fails.据我了解,UDP 协议没有定义如果数据损坏(即校验和失败)要采取的措施。 That is our application can make the packet to be retransmitted or let the packet be declared lost....那就是我们的应用程序可以让数据包被重传或者让数据包被宣告丢失......

While implementing Datagram Sockets in java I want to identify if the checksum is correct or not for some packet sent....在 java 中实现数据报 Sockets 时,我想确定发送的某些数据包的校验和是否正确......

Is there any way in java to do so... java 有什么办法可以这样做...

Basically I want that I come to know that this packet has been corrupted while transmission and thus has to be retransmitted....基本上我希望我知道这个数据包在传输时已损坏,因此必须重新传输....

Thanks a lot非常感谢

I'd check out the two following classes: CheckedInputStream and Checksum . 我将查看以下两个类: CheckedInputStreamChecksum A checksum should be performed by the machine sending the packet, and the machine receiving the packet should also perform a checksum, and then compare values. 校验和应该由发送数据包的机器执行,接收数据包的机器也应该执行校验和,然后比较值。 At least that's how I've seen it done.. 至少那是我看到它完成的方式..

Note: checksum must be included in packet being sent across. 注意:校验和必须包含在发送的数据包中。 Also, since you're checking if the data has been corrupted, ByteArrayInputStream may prove to be useful too. 此外,由于您正在检查数据是否已损坏, ByteArrayInputStream可能也证明是有用的。 Here's an example . 这是一个例子

First you have to add the checksum to the udp message. 首先,您必须将校验和添加到udp消息中。 I assume that the checksum has been put in front on the rest of the message and I assume that calcCheckSum can calculate the checksum. 我假设校验和已放在消息的其余部分的前面,我假设calcCheckSum可以计算校验和。

import java.net.*;

DatagramSocket socket = new DatagramSocket();
byte[] buffer = new byte[256]; // some appropriate size
DatagramPacket packet = new DatagramPacket(buf, buf.length);

socket.receive(packet);
String checksum = new String(packet.getData(),0, <some length>);
boolean ok = calcCheckSum(checksum);

The UDP header itself is short and of fixed size (8 bytes): UDP header 本身很短且大小固定(8 字节):

offset抵消 size尺寸 contents内容
0 0 16 bits 16 位 source port源端口
2 2 16 bits 16 位 destination port目的端口
4 4 16 bits 16 位 length长度
6 6 16 bits 16 位 checksum校验和

The checksum algorithm is defined in RFC 768 .校验和算法在RFC 768中定义。

So, talking of Java, the DatagramSocket/DatagramPackets should do the checksum calculation and checking.所以,说到 Java,DatagramSocket/DatagramPackets 应该做校验和计算和检查。

But it appears they do not.但似乎他们没有。 I dug into the implementation classes which still are in com.sun.我深入研究了仍在 com.sun 中的实现类。 legacy packages, and they seem to ignore checksum entirely.遗留包,它们似乎完全忽略了校验和。 No computing, no checking.没有计算,没有检查。

They also do not send an Exception or other notice to the user about a failure in the checksum.他们也不会向用户发送有关校验和失败的异常或其他通知。 Thus, the answer to the original questions are:因此,原始问题的答案是:

  • Java JDK implementation does not handle the UDP checksum at all. Java JDK 实现根本不处理 UDP 校验和。
  • So, no, in standard Java you cannot identify whether the UDP checksum is correct .所以,不,在标准 Java 中,您无法确定 UDP 校验和是否正确
  • You'll have to do your own checksumming in the packet payload data instead.您必须在数据包有效负载数据中进行自己的校验和。

And in the data, it is entirely up to you how you do that.在数据中,如何做到这一点完全取决于您。

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

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