Search This Blog

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

Tuesday 27 September 2011

Bitstuffing in java


Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientbs
{              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 count=0;
                                String s1[]=new String[100];
                                System.out.println("Enter the data");
                                for(int i=0;i<15;i++)
                                {              s1[i]=in.readLine();
                                                if(s1[i].equals("1"))
                                                {              count++;
                                                                if(count==5)
                                                                {              s1[i+1]="0";
                                                                                i++;
                                                                                count=0;                                }                              }
                                                else
                                                count=0;                }
                                System.out.println("The data is:");
                                for(int i=0;i<19;i++)
                                                System.out.println(s1[i]);
                                for(int i=0;i<19;i++)
                                {              dout.writeBytes(s1[i]+'\n'); }
                                String s2=din.readLine();
                                System.out.println(s2);
                                clsct.close();          }
                catch(Exception e)
                {              System.out.println(e);         }                              }}
                               
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverbs
{              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());
                                                int count=0;
                                                String st[]=new String[100];
                                                String s="Data recieved correctly";
                                                for(int i=0;i<19;i++)
                                                {st[i]=din.readLine();          }
                                                System.out.println("The data recieved is:");
                                                for(int i=0;i<19;i++)
                                                {              if(st[i].equals("1"))
                                                                {              count++;
                                                                                if(count==5)
                                                                                {              for(int j=i;j<19;j++)
                                                                                                st[j+1]=st[j+2];
                                                                                                count=0;                }              }
                                                                else
                                                                count=0;
                                                                if(st[i].equals("null"))
                                                                {              break;                    }
                                                                else
                                                                {              System.out.println(st[i]);                    }              }
                                                dout.writeBytes(s+'\n');
                                                obj.close();            }}
                catch(Exception e)
                {              System.out.println(e);         }              }}
Output
Client
Enter the data
1
1
1
1
1
1
1
1
1
1
1
1
1
The data is:
1
1
1
1
1
0
1
1
1
1
1
0
1
1
1
Server
The data recieved is:
1
1
1
1
1
1
1
1
1
1
1
1
1
Client
Data recieved correctly

No comments:

Post a Comment

Followers