question archive 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.

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
Please see the attached file for the complete solution

