Checksum is an error-detecting technique that can be applied to message of any length. It is used mostly at the network and transport layers of the TCP/IP protocol suite.
Here, we have considered decimal data that is being sent by the sender to the receiver using socket programming. The number of segments that the data is divided into here depends on the length of data being sent. If the length of data being sent is ‘x’, then the number of segments is also ‘x’, implying that each segment has single data. Here, we basically deal with decimal data. The concept will be consistent for string data as well because each character of the string can be represented by its equivalent ASCII code, hence again leaving us with decimal data.
At sender side : Enter data length 4 Enter data to send 67 43 0 22 Checksum Calculated is : 90 Data being sent along with Checksum..... Thanks for the feedback!! Message received Successfully! At receiver side : Data received (alond with checksum) is 67 43 0 22 90 Sum(in ones complement) is : 127 Calculated Checksum is : 0 Here the checksum calculated at the receiver side was 0. Hence, it indicates a successful data transfer.
At the Sender Side :
- First, ask for the length of data to send, in order to ascertain the number of segments.
- Then perform a ones complement of each data being entered simultaneously adding them. This means the sum would not be required to be complemented again.
- Then send the data along with the computed checksum to the server.
- Then report the successful transference of the message or otherwise depending on the feedback received from the server.
At the Receiver Side :
- The receiver waits for data to arrive from the sender.
- Once the data along with checksum is received from the sender, the receiver complements what is received and simultaneously keeps on adding them.
- Finally, the receiver complements the above sum, and checks whether the result is a zero or not and reports the same to the sender. A zero would indicate a successful data transfer and anything else would indicate an error in the data that is received.
Finally all connections are closed by both the sides.
Below is the implementation for above approach.
Here, “localhost” is used as the IP to setup the connection with port number 5000 opened for connection. The sender should start running prior and wait for the receiver.
- Implementing Generic Graph in Java
- Implementing next_permutation() in Java with Examples
- Implementing Byte Stuffing using Java
- Java | Implementing Iterator and Iterable Interface
- Implementing a Linked List in Java using Class
- Java Applet | Implementing Flood Fill algorithm
- Implementing our Own Hash Table with Separate Chaining in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.