Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Tuesday 13 September 2011

Program for Hamming code & CRC in Java


HAMMING CODE :
CLIENT
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;
class Clienthamming
{          public static void main(String args[])
            {
            try      
            {         
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        Socket clsct=new Socket("127.0.0.1",139);
                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
                        int x,y,z;
                        System.out.println("Enter the no.of bits in data word:");
                        String strr=in.readLine();
                        x=Integer.parseInt(strr);
                        System.out.println("Enter the no.of redundant bits:");

                        String str1=in.readLine();
                        y=Integer.parseInt(str1);
                        z=x+y;
                        double count[]=new double[100];
                        String ar[]=new String[100];
                        String arr[]=new String[100];
                        System.out.println("Enter the binary data:");
                        for(int j=1;j<=x;j++)
                        {
                                    ar[j]=in.readLine();
                        }
                        int k=x;
                        int w=1;
                        for(int j=3;j<=z;j++)
                        {         
                                    int a1=(int)Math.pow(2,w);
                                    System.out.println("power"+a1);
                                    if(j!=a1)
                                    {                                 
                                                arr[j]=ar[k];
                                                k--;
                                                if(arr[j].equals("1"))
                                                {
                                                            count[j]=1;
                                                }
                                                else
                                                            count[j]=0;
                                               
                                         
                                    }
                                    else
                                                count[j]=0;
                                    if(j-1==a1)
                                                  w++;

                                    System.out.println(arr[j]+" in position "+j);
                                   
                        }
                        for(int i=0;i<y;i++)
                        {          double d=0;
                                    int a=(int)Math.pow(2,i);
                                    switch(a)
                                    {
                                                case 1:
                                                for(int b=2;b<=z;b++)
                                                {
                                                            d+=count[b+1];
                                                            b++;
                                                }
                                                break;
                                                case 2:
                                                for(int b=2;b<=z;b++)
                                                {
                                                            d+=count[b]+count[b+1];
                                                            b++;b++;b++;
                                                }
                                                break;
                                                case 4:
                                                for(int b=4;b<=z;b++)
                                                {
                                                            d+=count[b]+count[b+1]+count[b+2]+count[b+3];
                                                            b++;b++;b++;b++;b++;b++;b++;
                                                }
                                                break;
                                                case 8:
                                                for(int b=8;b<=z;b++)
                                                {
                                                            d+=count[b]+count[b+1]+count[b+2]+count[b+3]+count[b+4]+count[b+5]+count[b+6]+count[b+7];
                                                            b=(b*i)-1;
                                                }
                                                break;                                     
                                                           
                                    }
                                    d%=2;
                                    if(d==0)
                                    arr[a]="0";
                                    else
                                    arr[a]="1";
                                   
                        }
                        dout.writeBytes(strr+'\n'+str1+'\n');
                        for(int i=1;i<=z;i++)
                        {
                                    dout.writeBytes(arr[i]+'\n');
                        }

                        String str=din.readLine();
                        System.out.println(str);                     
                        clsct.close();
            }
            catch (Exception e)
            {          System.out.println(e);            }
            }
}

SERVER
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;
class Serverhamming
{          public static void main(String args[])
            {
            try
            {         
                        ServerSocket obj=new ServerSocket(139);
                        Socket obj1=obj.accept();
                        while(true)
                        {         
                                    DataInputStream din=new DataInputStream(obj1.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    String str1="Data Received Correctly";
                                    String str2="Error in this position";
                                    double count[]=new double[100];
                                    String arr[]=new String[100];
                                    String s[]=new String[10];
                                    String strr=din.readLine();
                                    int x=Integer.parseInt(strr);
                                    System.out.println("No.of bits in Data word: "+x);
                                    String str11=din.readLine();
                                    int y=Integer.parseInt(str11);
                                    System.out.println("No.of Redundant bit: "+y);
                                    int z=x+y;
                                    System.out.println("No.of bits in code word:"+z);
                                    for(int i=1;i<=z;i++)
                                    {
                                                arr[i]=din.readLine();
                                                if(arr[i].equals("1"))
                                                {
                                                            count[i]=1;
                                                }
                                                else
                                                            count[i]=0;
                                    }

                                    for(int i=1;i<=z;i++)
                                    {          System.out.println(arr[i]+" in position "+1);}
                                   
                                    for(int j=0;j<y;j++)
                                    {
                                                double d=0;
                                                int a=(int)Math.pow(2,j);
                                                switch(a)
                                                {
                                                            case 1:
                                                                        for(int b=1;b<=z;b++)
                                                                        {
                                                                        int s1=b%2;
                                                                        if(s1!=0)
                                                                                    d+=count[b];
                                                                        }
                                                            break;
                                                            case 2:
                                                                        for(int b=2;b<=z;b++)
                                                                        {
                                                                        d+=count[b]+count[b+1];
                                                                        b++;b++;b++;
                                                                        }
                                                            break;
                                                case 4:
                                                                        for(int b=4;b<=z;b++)
                                                                        {
                                                                        d+=count[b]+count[b+1]+count[b+2]+count[b+3];
                                                                        b++;b++;b++;b++;b++;b++;b++;
                                                                        }
                                                            break;
                                                case 8:
                                                                        for(int b=8;b<=z;b++)
                                                                        {
                                                                        d+=count[b]+count[b+1]+count[b+2]+count[b+3]+count[b+4]+count[b+5]+count[b+6]+count[b+7];
                                                                        b=(b*j)-1;
                                                                        }
                                                            break;                                     
                                                           
                                                }
                                                d%=2;
                                                if(d==0)
                                                            count[a]=0;
                                                else
                                                            count[a]=1;
                                    }
                                    if(x<=4)s[y]="0";
                                    for(int j=0;j<y;j++)
                                    {
                                               
                                                int a=(int)Math.pow(2,j);
                                                if(count[a]==0)
                                                {
                                                            s[j]="0";         
                                                }
                                                else
                                                            s[j]="1";
                                                           
                                    }
                                    if(s[0].equals("0") && s[1].equals("0") && s[2].equals("0") && s[3].equals("0"))
                                                dout.writeBytes(str1+'\n');
                                    else
                                                dout.writeBytes(str2+" "+s[3]+s[2]+s[1]+s[0]+'\n');

                                    obj.close();
                        }
            }
            catch(Exception e)
            {          System.out.println(e);}
            }
}
OUTPUT
Client
Enter the no.of bits in data word:
7
Enter the no.of redundant bits:
4
Enter the binary data:
0
0
0
0
0
1
0
power2
0 in position 3
power4
null in position 4
power4
1 in position 5
power8
0 in position 6
power8
0 in position 7
power8
null in position 8
power8
0 in position 9
power16
0 in position 10
power16
0 in position 11
Server
No.of bits in Data word: 7
No.of Redundant bit: 4
No.of bits in code word:11
1 in position 1
0 in position 1
0 in position 1
1 in position 1
1 in position 1
0 in position 1
0 in position 1
0 in position 1
0 in position 1
0 in position 1
0 in position 1
Client
Data Received Correctly

CRC
Client
import java.io.*;
import java.net.*;
class crccli
{
            public static void main(String args[]) throws Exception
            {
                        byte b[]=new byte[1024];
                        DatagramSocket ds=new DatagramSocket(5000);
                        DatagramPacket dp=new DatagramPacket(b,1024);
                        String s;
                        StringBuffer s1=new StringBuffer(11);
                        int i,j=0;                     
                        int temp[]=new int[4];
                        int divisor[]=new int[4];
                        divisor[3]=1;
                        divisor[2]=1;
                        divisor[1]=0;
                        divisor[0]=1;
                        int dividend[]=new int[11];
                        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter a character: ");
                        s=buf.readLine();
                        System.out.println("The ASCII value of the character is "+(int)s.charAt(0));
                        s=Integer.toBinaryString((int)s.charAt(0));
                        System.out.println("The Binary equivalent is "+s);
                        i=s.length()-1;
                        for(j=0;j<3;j++)
                                    dividend[j]=0;
                        while(i>=0)
                        {
                                    if((int)s.charAt(i)==48)
                                    dividend[j]=0;
                                    else
                                    dividend[j]=1;
                                    i--;
                                    j++;
                        }
                        while(j<11)
                        {
                                    dividend[j]=0;
                                    j++;
                        }
                        System.out.print("The Dividend is ");
                        for(i=10;i>=0;i--)
                                    System.out.print(dividend[i]);
                        System.out.println("\n");
                        System.out.print("The divisor is ");
                        for(i=3;i>=0;i--)
                                    System.out.print(divisor[i]);
                        System.out.println("\n");
                        temp[3]=dividend[10];
                        temp[2]=dividend[9];
                        temp[1]=dividend[8];            
                        for(i=10;(i-3)>=0;i--)
                        {         
                                    temp[0]=dividend[i-3];                     
                                    if(temp[3]==0)
                                    {
                                                for(j=3;j>=0;j--)
                                                            temp[j]=temp[j]^0;
                                               
                                    }
                                    else
                                    {
                                                for(j=3;j>=0;j--)
                                                            temp[j]=temp[j]^divisor[j];
                                    }
                                    for(j=3;j>0;j--)
                                    temp[j]=temp[j-1];                            
                        }
                        System.out.println("The remainder is "+temp[3]+temp[2]+temp[1]);
                        dividend[2]=temp[3];
                        dividend[1]=temp[2];
                        dividend[0]=temp[1];            
                        for(i=0;i<11;i++)
                        {
                                    if(dividend[i]==0)
                                    s1.insert(i,"0");
                                    else
                                    s1.insert(i,"1");
                        }                     
                        s=s1.toString();         
                        System.out.println("The data to be sent is "+s1.reverse());                                     
                        b=s.getBytes();
                        ds.send(new DatagramPacket(b,b.length,InetAddress.getLocalHost(),5001));
                        ds.receive(dp);
                        String result=new String(dp.getData(),0,dp.getLength());
                        System.out.println(result);    
            }
}
Server
import java.io.*;
import java.net.*;
class crcser
{
            public static void main(String args[]) throws Exception
            {
                        byte b[]=new byte[1024];                 
                        DatagramSocket ds=new DatagramSocket(5001);
                        DatagramPacket dp=new DatagramPacket(b,1024);
                        String s=new String();                       
                        int i,j=0;                     
                        int temp[]=new int[4];
                        int divisor[]=new int[4];
                        divisor[3]=1;
                        divisor[2]=1;
                        divisor[1]=0;
                        divisor[0]=1;
                        int dividend[]=new int[11];
                        ds.receive(dp);
                        String s1=new String(dp.getData(),0,dp.getLength());
                StringBuffer s2=new StringBuffer(s1);
                System.out.println("Data received: "+s2.reverse());
                        for(i=0;i<11;i++)
                        {
                                    if((int)s1.charAt(i)==48)
                                    dividend[i]=0;
                                    else
                                    dividend[i]=1;
                        }                     
                        System.out.print("The divisor is ");
                        for(i=3;i>=0;i--)
                        System.out.print(divisor[i]);
                        System.out.println("\n");
                        temp[3]=dividend[10];
                        temp[2]=dividend[9];
                        temp[1]=dividend[8];            
                        for(i=10;(i-3)>=0;i--)
                        {         
                                    temp[0]=dividend[i-3];                     
                                    if(temp[3]==0)
                                    {
                                                for(j=3;j>=0;j--)
                                                            temp[j]=temp[j]^0;
                                               
                                    }
                                    else
                                    {
                                                for(j=3;j>=0;j--)
                                                            temp[j]=temp[j]^divisor[j];
                                    }
                                    for(j=3;j>0;j--)
                                                temp[j]=temp[j-1];                            
                        }         
                        System.out.println("The remainder is "+temp[3]+temp[2]+temp[1]); 
                        if(temp[3]==0&&temp[2]==0&&temp[1]==0)
                        {
                                    System.out.println("No errror in packet");
                                    s="Packet received at server without error";
                        }
                        else
                        {
                                    System.out.println("Errror in packet");
                                    s="Packet received at server with error";
                         }                                                        
                        b=s.getBytes();
                        ds.send(new DatagramPacket(b,b.length,InetAddress.getLocalHost(),5000));           
            }
}

Output
Enter a character:
0
The ASCII value of the character is 48
The Binary equivalent is 110000
The Dividend is 00110000000

The divisor is 1101

The remainder is 011
The data to be sent is 00110000011
Server
Data received: 00110000011
The divisor is 1101

The remainder is 000
No errror in packet
Client
Packet received at server without error

No comments:

Post a Comment

Followers