Computer Networking Lab

 Computer Network Lab:


EXP-1

Error detection in a packet using Checksum

A checksum is an error-detecting technique that can be applied to a 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 the Sender Side : 

1. First, ask for the length of data to send, in order to ascertain the number of segments.

2. Then perform a ones complement of each data being entered simultaneously adding them. This means the sum would not be required to be complimented again.

3. Then send the data along with the computed checksum to the server.

4. Then report the successful transference of the message or otherwise depending on the feedback received from the server.

At the Receiver Side :

1. The receiver waits for data to arrive from the sender.

2. Once the data along with checksum is received from the sender, the receiver complements what is received and simultaneously keeps on adding them.

3. 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 sides.

Below is the implementation for the above approach. 

Here, “localhost” is used as the IP to set up the connection with port number 5000 opened for connection. The sender should start running prior and wait for the receiver.  

                        // Java code for Checksum_Sender
package checksum_sender;
import java.io.*;
import java.net.*;
import java.util.*;
public class Checksum_Sender
{
    private int MAX = 100;
    private Socket socket = null;
    private ServerSocket servsock = null;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    public Checksum_Sender(int port) throws IOException
    {
        servsock = new ServerSocket(port);
        socket = servsock.accept();
        dis = new DataInputStream(socket.getInputStream());
        dos = new DataOutputStream(socket.getOutputStream());
        while (true)
        {
            int i, l, sum = 0, nob;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter data length");
            l = sc.nextInt();
            int data[] = new int[MAX];
            int c_data[] = new int[MAX];
            System.out.println("Enter data to send");
            for (i = 0; i < l; i++)
            {
                data[i] = sc.nextInt();
                nob = (int)(Math.floor(Math.log(data[i]) / Math.log(2))) + 1;
                c_data[i] = ((1 << nob) - 1) ^ data[i];
                sum += c_data[i];
            }
            data[i] = sum;
            l += 1;
            System.out.println("Checksum Calculated is : " + sum);
            System.out.println("Data being sent along with Checksum.....");
            dos.writeInt(l);
            for (int j = 0; j < l; j++)
                dos.writeInt(data[j]);
            if (dis.readUTF().equals("success"))
            {  
                System.out.println("Thanks for the feedback!! Message received Successfully!");
                break;
            }
            else if (dis.readUTF().equals("failure"))
            {
                System.out.println("Message was not received successfully!");
                break;
            }
        }
        dis.close();
        dos.close();
        socket.close();
    }
    public static void main(String args[]) throws IOException
    {
        Checksum_Sender cs = new Checksum_Sender(45678);
    }
}

 

                // Java code for Checksum_Receiver
package checksum_sender;
import java.net.*;
import java.io.*;
import java.util.*;
public class Checksum_Receiver
{
    private Socket s = null;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    public Checksum_Receiver(InetAddress ip,int port)throws IOException
    {
        s = new Socket(ip,port);
        dis = new DataInputStream(s.getInputStream());
        dos = new DataOutputStream(s.getOutputStream());
        while (true)
        {
            Scanner sc = new Scanner(System.in);
            int i, l, nob, sum = 0, chk_sum;
            l = dis.readInt();
            int c_data[] = new int[l];
            int data[] = new int[l];
            System.out.println("Data received (alond with checksum) is");
            for(i = 0; i< data.length; i++)
            {  
                data[i] = dis.readInt();
                System.out.println(data[i]);
                nob = (int)(Math.floor(Math.log(data[i]) / Math.log(2))) + 1;
                c_data[i] = ((1 << nob) - 1) ^ data[i];
                sum += c_data[i];
            }
            System.out.println("Sum(in ones complement) is : "+sum);
            nob = (int)(Math.floor(Math.log(sum) / Math.log(2))) + 1;
            sum = ((1 << nob) - 1) ^ sum;
            System.out.println("Calculated Checksum is : "+sum);
            if(sum == 0)
            {  
                dos.writeUTF("success");
                break;
            }    
            else
            {  
                dos.writeUTF("failure");
                break;
            }
        }
        dis.close();
        dos.close();
        s.close();
    }
    public static void main(String args[])throws IOException
    {  
        InetAddress ip = InetAddress.getLocalHost();
        Checksum_Receiver cr = new Checksum_Receiver(ip,5000);
    }  
}


Outputs:

No comments:

Post a Comment

Jio, Airtel and VI Price Hike 2021