Sunday 1 November 2015

Bit Stuffing in Computer Networks - Java Program - Client Server

/* In the user data, if 5 consecutive 1s come ,we include 0 after it and send the code to server where
the inversion operation takes place and data are read.. */



CLIENT:

import java.util.Scanner;
import java.util.Arrays;
import java.net.*;
import java.io.*;

class BitStuffCli
{
public static char convert(int i)
{
if(i==1)
return '1';
else
return '0';
}

public static void main(String args[]) throws Exception
{
int a[]=new int[100];
int b[]=new int[120];
int count=0;
int n,i,j;
Scanner in=new Scanner(System.in);
System.out.println("Enter no of bits:");
n=in.nextInt();
System.out.println("Enter the bits:");
for( i=0;i<n;i++)
a[i]=in.nextInt();

for(i=0,j=0;i<n;i++,j++)
{
b[j]=a[i];
if(a[i]==1)
count++;
else
count=0;
if(count==5)
{
j++;
b[j]=0;
count=0;
}
}
String s=new String();
System.out.println("\nSent as:");
for(i=0;i<j;i++)
{
System.out.print(b[i]);
s+=convert(b[i]);
}


Socket socket = new Socket("localhost",1234);
OutputStream ostream = socket.getOutputStream();
ostream.write(s.getBytes());
}
}

SERVER:

import java.util.Scanner;
import java.util.Arrays;
import java.net.*;
import java.io.*;

class BitStuffSer
{
public static int reconvert(char c)
if(c=='1')
return 1;
else 
return 0;

}
public static void main(String args[]) throws Exception
{
int i,j,count,n;
int b[]=new int[120];
int c[]=new int[100];
byte bytes[]=new byte[1000];
ServerSocket ss = new ServerSocket(1234);
Socket socket = new Socket();
socket=ss.accept();
InputStream istream = socket.getInputStream();
istream.read(bytes);
String s = new String(bytes);
    s=s.trim();
System.out.println("Received as:"+s);
n=s.length();
for(i=0;i<n;i++)
{
b[i]=reconvert(s.charAt(i));
}
 
for(i=0,j=0,count=0;j<n;i++,j++)
{
c[i]=b[j];
if(b[j]==1)
count++;
else
count=0;
if(count==5)
{
j++;
count=0;
}
}
System.out.println("\nOriginal bits:");
for(j=i,i=0;i<j;i++)

System.out.print(c[i]);
}
}


OUTPUT:
C:\Marsh\cn>java BitStuffCli
Enter no of bits:
12
Enter the bits:
1 1 0 1 1 1 1 1 0 1 0 1

Sent as:
1101111100101


C:\Marsh\cn>java BitStuffSer
Received as:1101111100101

Original bits:
110111110101



4 comments:

  1. plz implement byte stuffing in java program

    ReplyDelete
  2. can anyone explain the client part of the code?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. a[] is input and b[] is after stuffing.so here we were searching whether there were 5 consicutive 1's we added a extra 0's and then all the same.because here is the rule that there shouldn't be 6 consicutive 1's in the sending message at any cost.so we just checked and added a extra 0 to maintain the rule of stuffing and to remove confussion.I said confussion because u could say why I would put extra 0 if there were ".. 111110.." bcz there were no 6 consicutive 1's.but while de stuffing(convert to real input) you will be in confussion that the 6th 0 is whether for stuffing or not.
    sorry in advance for any mistake,I was also searching this material and explained what I just have understood

    ReplyDelete