Search This Blog

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

Thursday, 11 August 2011

Program for Parity bit Generator and Checker using TCP/IP


Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientparity
{          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());

                        System.out.println("Enter the binary data:");
                        int count=0;
                        String arr[]=new String[8];
                        for(int i=0;i<7;i++)
                        {
                                    arr[i]=in.readLine();
                                    if(arr[i].equals("1"))
                                    {
                                                count++;
                                    }
                        }
                        int x;
                        System.out.println("Press \n 0 to assign parity automatically \n 1 to assign parity manualy");
                        x=Integer.parseInt(in.readLine());
                        switch(x)
                        {
                                    case 0:
                                                count=count%2;
                                                if(count==0)
                                                {
                                                            arr[7]="0";
                                                }
                                                else
                                                {
                                                            arr[7]="1";
                                                }
                                                break;
                                    case 1:
                                                System.out.println("Enter the parity bit:");
                                                arr[7]=in.readLine();
                                                break;
                        }
                        for(int i=0;i<8;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.*;
class Serverparity
{          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 str[]=new String[8];
                                    String str1="Data Received Correctly";
                                    String str2="Retransmit the data";

                                    int count=0;
                                    for(int i=0;i<7;i++)
                                    {
                                                str[i]=din.readLine();
                                                if(str[i].equals("1"))
                                                {
                                                            count++;
                                                }
                                    }
                                    count%=2;
                                    str[7]=din.readLine();
                                    if(count==0)
                                    {
                                                if(str[7].equals("0"))
                                                {
                                                dout.writeBytes(str1+'\n');
                                                }
                                                else
                                                {dout.writeBytes(str2+'\n');}
                                    }
                                    else
                                    {
                                                if(str[7].equals("1") )
                                                {
                                                            count++;
                                                            dout.writeBytes(str1+'\n');
                                                }
                                                else
                                                {dout.writeBytes(str2+'\n');}
                                    }
                                   
                                    for(int i=0;i<8;i++)
                                    {          System.out.println(str[i]);}
                                    count%=2;      
                                    System.out.println("Syndrome:"+count);
                                    obj.close();}
                        }
            catch(Exception e)
            {          System.out.println(e);}
            }
}
Output
E:\networks\parity>java Serverparity
E:\networks\parity>java Clientparity
Enter the binary data:
1
0
0
0
1
0
0
Press
 0 to assign parity automatically
 1 to assign parity manualy
0
Server
1
0
0
0
1
0
0
0
Syndrome:0
Client
Data Received Correctly

Enter the binary data:
1
0
0
0
1
0
1
Press
 0 to assign parity automatically
 1 to assign parity manualy
1
Enter the parity bit:
0

Server
1
0
0
0
1
0
1
0
Syndrome:1
Client
Retransmit the data

1 comment:

  1. Compile el programa, pero no puedo introducir los 7 bits, solo puedo poner 4, porque?

    ReplyDelete

Followers