question archive One of the logical fields of TCP is the checksum field

One of the logical fields of TCP is the checksum field

Subject:Computer SciencePrice:7.86 Bought12

One of the logical fields of TCP is the checksum field. If it doesn't match at the destination, an error will be detected. Hence, Give a program (any programming language) that reads information of a TCP segment and calculates the checksum.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Program Code

 

import java.util.*;
//create class
public class ChecksumImplementation
{
	//static method to generate checksum
   static int generate_checksum(String s)
   {
	   //declare variables
      String my_hex_val = new String();
      int x, i, my_checksum = 0;
      //for loop iterate till the length of the passed string s -2
      for (i = 0; i < s.length() - 2; i = i + 2)
      {
    	 //convert character in the string as integer at index i
         x = (int) (s.charAt(i));
         //convert into hexadecimal value
         my_hex_val = Integer.toHexString(x);
         //convert character in the string as integer at index i+1
         x = (int) (s.charAt(i + 1));
         //add the hexadecimal value of character at index i and i+1
         my_hex_val = my_hex_val + Integer.toHexString(x);
         //print the characters at index i and i+1 and the sum of the hexadecimal value
         System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : " + my_hex_val);
         //convert into integer
         x = Integer.parseInt(my_hex_val, 16);
         //add the x with my_checksum
         my_checksum += x;
      }
      //checks the string length is even
      if (s.length() % 2 == 0)
      {
    	 //convert character into integer at index i
         x = (int) (s.charAt(i));
         //integer to hexadecimal
         my_hex_val = Integer.toHexString(x);
         //convert character into inetger at index i+1
         x = (int) (s.charAt(i + 1));
         //integer to hexadecimal and sum the values of two characters
         my_hex_val = my_hex_val + Integer.toHexString(x);
         //print the character and the hexadecimal values
         System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "+ my_hex_val);
         //convert into integer
         x = Integer.parseInt(my_hex_val, 16);
      } 
      else //the string length is odd 
      {
    	 //convert character into inetger at index i
         x = (int) (s.charAt(i));
         //add the hexadecimal value of x with xx
         my_hex_val = "00" + Integer.toHexString(x);
         //convert into integer
         x = Integer.parseInt(my_hex_val, 16);
         //print the values
         System.out.println(s.charAt(i) + " : " + my_hex_val);
      }
      //add the checksum with x
      my_checksum += x;
      //convert into hexadecimal
      my_hex_val = Integer.toHexString(my_checksum);
      //check the length of hexadecimal value greater than 4
      if (my_hex_val.length() > 4)
      {
    	 //convert into integer
         int carry = Integer.parseInt(("" + my_hex_val.charAt(0)), 16);
         //create a substring from index 1 to 5 in the hexadecimal value
         my_hex_val = my_hex_val.substring(1, 5);
         //convert into integer
         my_checksum = Integer.parseInt(my_hex_val, 16);
         //add the carry values
         my_checksum += carry;
      }
      //get the checksum value by calling the method generate_complement()
      my_checksum = generate_complement(my_checksum);
      //return the checksum
      return my_checksum;
   }
   //method to get and check the value and checksum from the receiver end
   static void receive(String s, int my_checksum)
   {
	  //the checksum is generated for the given string
      int gen_checksum = generate_checksum(s);
      //find the complement for the checksum
      gen_checksum = generate_complement(gen_checksum);
      //add the checksum and the ones complement of the value
      int syndrome = gen_checksum + my_checksum;
      //call the method to create complement
      syndrome = generate_complement(syndrome);
      //print the value
      System.out.println("The value of syndrome is " + Integer.toHexString(syndrome));
      //if the value is zero then the data received successfully
      if (syndrome == 0)
      {
         System.out.println("Data has been received without any errors");
      } 
      //the data is received with the error
      else 
      {
         System.out.println("An error was encountered in the received data");
      }
   }
   //method to generate complement
   static int generate_complement(int my_checksum)
   {
	  //complement for the given checksum vaue
      my_checksum = Integer.parseInt("FFFF", 16) - my_checksum;
      return my_checksum;
   }
	//main class
   public static void main(String args[])
   {
	   //initialize scanner object
      Scanner my_scan = new Scanner(System.in);
      //ask user for the input string
      System.out.println("Enter the input string ");
      //assign the value in string variable my-in
      String my_in = my_scan.next();
      //the return value from the method generate_checksum() to the integer variable my_checksum
      int my_checksum = generate_checksum(my_in);
      //print the statements based on the checksum value returned
      System.out.println("The checksum that has been generated is " + Integer.toHexString(my_checksum));
      //get the send data from the user
      System.out.println("Enter the data that needs to be sent to the receiver ");
      my_in = my_scan.next();
      //get the checksum from the user 
      System.out.println("Enter the checksum that needs to be sent to the receiver ");
      my_checksum = Integer.parseInt((my_scan.next()), 16);
      //call the method receive
      receive(my_in, my_checksum);
      //close the scanner object
      my_scan.close();
   }
}

Program Description

 

  • Create the class ChecksumImplementation with the methods generate_checksum(), generate_complement(), and the receiver().
  • The generate_checksum() method used to generate the checksum for the string passed as a parameter.
  • Every character in the string is converted into an integer value then that integer value is converted into a hexadecimal value. 
  • Adding the values of every character in the string.
  • Pass the final value to the generate_complement() method to generate the complement for the value. 
  • Now from the sender side data and the checksum for the data is generated.
  • Call the method receiver() with the parameters sender data and the checksum.
  • Here for the sender data, the checksum is found by calling the generate_checksum() then finding the complement value.
  • The value is zero then the value sent by the sender is received by the receiver.
  • Otherwise, the sender value is different from the receiver value then the checksum fails.

Please see the attached file for the complete solution