Hi, i wrote checksum method to ensure that there are no errors in the header and data in TCP.
The technique calculates the values of the checksum field at the sender side, and stores the result in the checksum field. When the message reaches the destination, the receiver recalculates the checksum. If the final result is all zeroes, this designates that there is no errors in the message. when i run my program the result is not right i don't know where is the problem
the sender and receiver should input message
if the sender and receiver input the same massage the checksum should be zero
thank u :)
The technique calculates the values of the checksum field at the sender side, and stores the result in the checksum field. When the message reaches the destination, the receiver recalculates the checksum. If the final result is all zeroes, this designates that there is no errors in the message. when i run my program the result is not right i don't know where is the problem
the sender and receiver should input message
if the sender and receiver input the same massage the checksum should be zero
thank u :)
Code:
import java.util.*; public class NW242 { static Scanner console=new Scanner (System.in); public static void main (String[]args) { int count=0; int carry=0; char D=' '; char U=' '; String sumw=""; String sumw1=""; String St=""; int index=3; String ST=""; String w=""; String z=""; int cu=0; boolean F=false; String AS=""; String[]arr1=new String[AS.length()]; String []arr2=new String[arr1.length]; int [] check=new int[4]; int wi=0; String Str=""; String S=""; int co=0; int ro=0; int CB=0; int col=0; int i=0; int M=0; int row=0; int Carry=0; String [] CH=new String [4]; int h=3; int c=4; int g=3; int p=0; String d=""; int value; boolean Fond=false; int sum=0; ////////////////////////////////////////////////////////////////////////////////////////////reciv int count1=0; int carry1=0; char D1=' '; char U1=' '; boolean FO1=false; int index1=3; String ST1=""; String w1=""; String AS1=""; int [] check1=new int[4]; String [] CH1=new String [4]; String Str1=""; String S1=""; int co1=0; int ro1=0; int col1=0; int i1=0; int cou=0; int M1=0; int R=0; int row1=0; int Carry1=0; int h1=3; int c14; char z1=' '; int g1=3; int p1=0; String d1=""; int value1; int sum1=0; System.out.println("Enter the Massege"); String sender=console.nextLine(); int len=sender.length(); String q=""; if(len%2==0) for ( i = 0; i < sender.length();i ++) { U = sender.charAt(i); int j = (int) U; String hexstr = Integer.toHexString(j); //hex AS=AS.concat(hexstr); }//end for else q=sender.concat("0"); for ( i = 0; i < q.length();i ++) { U = q.charAt(i); int j = (int) U; String hexstr = Integer.toHexString(j); //hex AS=AS.concat(hexstr); } System.out.println("ASCII value of "+"" +" is AS" +" " + AS + "."); System.out.println(" Full Array "); int r=AS.length()/4; String [][] arr=new String [r][c]; int lenn=arr.length; for(row = 0 ;row <r;row++) { for( col=0;col<4;col++) { char ch=AS.charAt(4*row+col); String s = String.valueOf(ch); arr[row][col]=s; System.out.print(arr[row][col]); } System.out.println(""); } for(co=3;co>=ro;co--) {// star for(ro=0;ro<r;ro++) { ST=arr[ro][co]; switch(ST.charAt(0)) { case '0': M=0; break; case '1': M=1; break; case '2': M=2; break; case '3': M=3; break; case '4': M=4; break; case '5': M=5; break; case '6': M=7; break; case '8': M=8; break; case '9': M=9; break; case 'A': case 'a': M=10; break; case 'B': case 'b': M=11; break; case 'C': case 'c': M=12; break; case 'D': case 'd': M=13; break; case 'E': case 'e': M=14; break; case 'F': case 'f': M=15; break; }//end switch sum=sum+M; sum=sum+carry; carry=0; } System.out.println("The sum "+" "+ (ro) + "=" +sum); while(sum>=16) { sum=sum-16; Carry++; System.out.println("the carry is " + Carry); } switch(sum) { //start case 0:CH[index]="0"; System.out.println(CH[index]); break; case 2:CH[index]="2"; System.out.println(CH[index]); break; case 3:CH[index]="3"; System.out.println(CH[index]); break; case 4:CH[index]="4"; System.out.println(CH[index]); break; case 5:CH[index]="5"; System.out.println(CH[index]); break; case 6: {CH[index]="6"; System.out.println( " CH =" + CH[index]);} break; case 7:CH[index]="7"; System.out.println(CH[index]); break; case 8:CH[index]="8"; System.out.println(CH[index]); break; case 9:CH[index]="9";System.out.println(CH[index]); break; case 10:CH[index]="A"; System.out.println(CH[index]); break; case 11:CH[index]="B"; System.out.println(CH[index]); break; case 12:CH[index]="C"; System.out.println(CH[index]); break; case 13:CH[index]="D"; System.out.println(CH[index]); break; case 14:CH[index]="E"; System.out.println(CH[index]); break; case 15:CH[index]="F"; System.out.println(CH[index]); break; } index--; System.out.println(" Sum is " + sum); sumw=Integer.toString(sum); sum=0; ro=0; } // smallest for while(Carry!=0) { for( h=3;h>=0;h--) { CH[h]=sumw; switch(CH[h].charAt(0)) { // from A 2 F and from 0 2 9 case 'A': case 'a': sum=10+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case 'B': case 'b': sum=11+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case 'C': case 'c': sum=12+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case 'D': case 'd': sum=13+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case 'E': case 'e': sum=14+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case 'F': case 'f': sum=15+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '0': sum=0+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '1': sum=1+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '2': sum=2+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '3': sum=3+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; case '4': sum=4+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '5': sum=5+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '6': sum=6+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '7': sum=7+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '8': sum=8+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; case '9': sum=9+Carry; Carry=0; while (sum>=16) { sum=sum-16; Carry++; } check[h]=sum; break; }//swi }//whi // } } // the big for col for( g=3;g>=0;g--) // checksum { check[g]=15-check[g]; } for(p=0;p<=3;p++) // in hex { switch(check[p]) { case 0:d=d+"0"; break; case 1:d=d+"1"; break; case 2:d=d+"3"; break; case 4:d=d+"4"; break; case 5:d=d+"5"; break; case 6:d=d+"6"; break; case 7:d=d+"7"; break; case 8:d=d+"8"; break; case 9:d=d+"9"; break; case 10:d=d+"A"; break; case 11:d=d+"B"; break; case 12:d=d+"C"; break; case 13:d=d+"D"; break; case 14:d=d+"E"; break; case 15:d=d+"F"; break; }// end for }//end wihle System.out.println("the check sum is " + d); System.out.println("the check sum is " + d); String [] B=new String [d.length()]; for(int y=3;y>=d.length();y--) { String MN=B[y]; } System.out.println("Enter the Massege");// reciver Message String recive=console.nextLine(); int len1=recive.length(); String q1=""; if(len1%2==0) for ( i1 = 0; i1 < recive.length();i1 ++) { U1 = recive.charAt(i1); int j1 = (int) U1; String hexstr = Integer.toHexString(j1); //hex AS1=AS1.concat(hexstr); }//end for else q1=recive.concat("0"); for ( i1 = 0; i1< q1.length();i1 ++) { U1 = q1.charAt(i1); int j1 = (int) U1; String hexstr = Integer.toHexString(j1); //hex AS1=AS1.concat(hexstr); } System.out.println("ASCII value of "+"" +" is AS" +" " + AS1 + "."); System.out.println(" Full Array "); String[]arr11=new String[AS1.length()]; String []arr21=new String[arr11.length]; int r1=AS1.length()/4; int c1=4; String [][] arr3=new String [r1][c1]; int lenn1=arr3.length; for(row1 = 0 ;row1 <r1;row1++) { for( col1=0;col1<4;col1++) { char ch=AS1.charAt(4*row1+col1); String s = String.valueOf(ch); arr3[row1][col1]=s; System.out.print(arr3[row1][col1]); } System.out.println(""); } for(co1=3;co1>=ro1;co1--) {// star for( ro1=0;ro1<r1;ro1++) { ST1=arr3[ro1][co1]; char FM=ST1.charAt(0); switch(FM) { case '0': M1=0; break; case '1': M1=1; break; case '2': M1=2; break; case '3': M1=3; break; case '4': M1=4; break; case '5': M1=5; break; case '6': M1=7; break; case '8': M1=8; break; case '9': M1=9; break; case 'A': case 'a': M1=10; break; case 'B': case 'b': M1=11; break; case 'C': case 'c': M1=12; break; case 'D': case 'd': M1=13; break; case 'E': case 'e': M1=14; break; case 'F': case 'f': M1=15; break; } for(int y=3;y>=d.length();y--) { String MN=B[y]; switch(MN.charAt(0)) { case '0': R=0; break; case '1': R=1; break; case '2': R=2; break; case '3': R=3; break; case '4': R=4; break; case '5': R=5; break; case '6': R=7; break; case '8': R=8; break; case '9': R=9; break; case 'A': case 'a': R=10; break; case 'B': case 'b': R=11; break; case 'C': case 'c': R=12; break; case 'D': case 'd': R=13; break; case 'E': case 'e': R=14; break; case 'F': case 'f': R=15; break; }//end switch sum1=sum1+M1+R; sum1=sum1+carry1; carry1=0; }// end for } System.out.println("The sum "+" "+ "=" +sum1); while(sum1>=16) { sum1=sum1-16; Carry1++; System.out.println("the carry is " + Carry1); }//end while switch(sum1) { case 0:CH1[index1]="0"; break; case 1:CH1[index1]="1"; break; case 2:CH1[index1]="2"; break; case 3:CH1[index1]="3"; break; case 4:CH1[index1]="4"; break; case 5:CH1[index1]="5"; break; case 6:CH1[index1]="6"; break; case 7:CH1[index1]="7"; break; case 8:CH1[index1]="8"; break; case 9:CH1[index1]="9"; break; case 10:CH1[index1]="A"; break; case 11:CH1[index1]="B"; break; case 12:CH1[index1]="C"; break; case 13:CH1[index1]="D"; break; case 14:CH1[index1]="E"; break; case 15:CH1[index1]="F"; break; } index1--; sumw1=Integer.toString(sum); ro1=0; sum1=0; }//end wh while(Carry1 !=0) { for( h1=3;h1>=0;h1--) { CH1[h1]=sumw1; switch(sumw1.charAt(0)) { case 'A': case 'a': sum1=10+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case 'B': case 'b': sum1=11+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case 'C': case 'c': sum1=12+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case 'D': case 'd': sum1=13+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case 'E': case 'e': sum1=14+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case 'F': case 'f': sum1=15+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '0': sum1=0+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '1': sum1=1+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '2': sum1=2+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '3': sum1=3+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; case '4': sum1=4+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '5': sum1=5+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '6': sum1=6+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '7': sum1=7+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '8': sum1=8+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; break; case '9': sum1=9+Carry1; Carry1=0; while (sum1>=16) { sum1=sum1-16; Carry1++; } check1[h1]=sum1; } break; //} } }// bigest for for( g1=3;g1>=0;g1--) { check1[g1]=15-check1[g1]; } for(p1=0;p1<=3;p1++) { switch(check1[p1]) { case 10:d1=d1+"A"; break; case 11:d1=d1+"B"; break; case 12:d1=d1+"C"; break; case 13:d1=d1+"D"; break; case 14:d1=d1+"E"; break; case 15:d1=d1+"F"; break; case 0:d1=d1+"0"; break; case 1:d1=d1+"1"; break; case 2:d1=d1+"2"; break; case 3:d1=d1+"3"; break; case 4:d1=d1+"4"; break; case 5:d1=d1+"5"; break; case 6:d1=d1+"6"; break; case 7:d1=d1+"7"; break; case 8:d1=d1+"8"; break; case 9:d1=d1+"9"; break; }// end if }//end wihle System.out.println("the check sum is " + d1);// is check sum if(d1.equals("0000")) System.out.println(" The Message is correct"); else System.out.println(" The Message is not correct"); } }
Comment