Checksum in UDP Datagram Socket + java

As far as I understand, UDP does not determine the action to be taken if the data is corrupted, i.e. the checksum fails. This application of ours can make the package resubmitted or let the package be declared lost ....

When implementing Datagram Sockets in java, I want to determine if the checksum is correct or not for the sent packet ....

Is there a way in java for this ...

Basically, I want me to find out that this packet was corrupted during transmission and therefore needs to be retransmitted ...

thanks a lot

+4
source share
2 answers

I checked the following two classes: CheckedInputStream and Checksum . The checksum must be performed by the machine sending the packet, and the machine receiving the packet must also perform the checksum, and then compare the values. At least as I saw it.

Note: The checksum must be included in the package sent through. Also, since you are checking to see if the data has been corrupted, a ByteArrayInputStream may be useful. Here is an example .

+3
source

First you need to add the checksum to the udp message. I am assuming that a checksum has been inserted before the rest of the message, and I am assuming calcCheckSum can calculate the checksum.

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); 
0
source

Source: https://habr.com/ru/post/1339373/


All Articles