tcp checksum calculation java code

Solutions on MaxInterview for tcp checksum calculation java code by the best coders in the world

showing results for - "tcp checksum calculation java code"
Ronan
21 Oct 2016
1// Java code for Checksum_Sender 
2package checksum_sender; 
3  
4import java.io.*; 
5import java.net.*; 
6import java.util.*; 
7  
8public class Checksum_Sender  
9{ 
10    // Setting maximum data length 
11    private int MAX = 100; 
12      
13    // initialize socket and I/O streams 
14    private Socket socket = null; 
15    private ServerSocket servsock = null; 
16    private DataInputStream dis = null; 
17    private DataOutputStream dos = null; 
18      
19    public Checksum_Sender(int port) throws IOException 
20    { 
21        servsock = new ServerSocket(port); 
22          
23        // Used to block until a client connects to the server 
24        socket = servsock.accept(); 
25          
26        dis = new DataInputStream(socket.getInputStream()); 
27        dos = new DataOutputStream(socket.getOutputStream()); 
28          
29        while (true30        { 
31            int i, l, sum = 0, nob; 
32            Scanner sc = new Scanner(System.in); 
33            System.out.println("Enter data length"); 
34            l = sc.nextInt(); 
35              
36            // Array to hold the data being entered 
37            int data[] = new int[MAX]; 
38              
39            // Array to hold the complement of each data 
40            int c_data[] = new int[MAX]; 
41              
42            System.out.println("Enter data to send"); 
43              
44            for (i = 0; i < l; i++)  
45            { 
46                data[i] = sc.nextInt(); 
47                  
48                // Complementing the entered data 
49                // Here we find the number of bits required to represent 
50                // the data, like say 8 requires 1000, i.e 4 bits 
51                nob = (int)(Math.floor(Math.log(data[i]) / Math.log(2))) + 1; 
52                  
53                // Here we do a XOR of the data with the number 2^n -1, 
54                // where n is the nob calculated in previous step 
55                c_data[i] = ((1 << nob) - 1) ^ data[i]; 
56                  
57                // Adding the complemented data and storing in sum 
58                sum += c_data[i]; 
59            } 
60              
61            // The sum(i.e checksum) is also sent along with the data 
62            data[i] = sum; 
63            l += 1; 
64              
65            System.out.println("Checksum Calculated is : " + sum); 
66            System.out.println("Data being sent along with Checkum....."); 
67              
68            // Sends the data length to receiver 
69            dos.writeInt(l); 
70              
71            // Sends the data one by one to receiver 
72            for (int j = 0; j < l; j++) 
73                dos.writeInt(data[j]); 
74              
75            // Displaying appropriate message depending on feedback received 
76            if (dis.readUTF().equals("success"))  
77            {    
78                System.out.println("Thanks for the feedback!! Message received  
79                                   Successfully!"); 
80                break; 
81            } 
82              
83            else if (dis.readUTF().equals("failure"))  
84            { 
85                System.out.println("Message was not received successfully!"); 
86                break; 
87            } 
88        } 
89          
90        // Closing all connections 
91        dis.close(); 
92        dos.close(); 
93        socket.close(); 
94    } 
95  
96    // Driver Method 
97    public static void main(String args[]) throws IOException 
98    { 
99        Checksum_Sender cs = new Checksum_Sender(45678); 
100    } 
101} 
102