0% found this document useful (0 votes)
131 views1 page

Java Bit Stuffing Client Code

This Java program implements bit stuffing by taking user input of unstuffed data, counting consecutive 1s, inserting a 0 after every 5 1s, adding start and end flag bytes, and sending the stuffed data to a server for unstuffing. The client opens a socket connection to a server, takes user input, stuffs the data by adding 0s after runs of 5 1s, prepends and appends flag bytes, and writes the stuffed data to the server for processing.

Uploaded by

Arkadip Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views1 page

Java Bit Stuffing Client Code

This Java program implements bit stuffing by taking user input of unstuffed data, counting consecutive 1s, inserting a 0 after every 5 1s, adding start and end flag bytes, and sending the stuffed data to a server for unstuffing. The client opens a socket connection to a server, takes user input, stuffs the data by adding 0s after runs of 5 1s, prepends and appends flag bytes, and writes the stuffed data to the server for processing.

Uploaded by

Arkadip Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import java.io.

*;
import java.net.*;
import java.util.Scanner;
public class BitStuffingClient {
public static void main(String[] args) throws IOException
{
// Opens a socket for connection
Socket socket = new Socket("localhost", 1234);

DataInputStream dis = new DataInputStream(socket.getInputStream());


DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

// Scanner class object to take input


Scanner sc = new Scanner(System.in);

// Takes input of unstuffed data from user


System.out.println("Enter data: ");
String data = sc.nextLine();

int cnt = 0;
String s = "";
for (int i = 0; i < data.length(); i++) {
char ch = data.charAt(i);
if (ch == '1') {

// count number of consecutive 1's


// in user's data
cnt++;

if (cnt < 5)
s += ch;
else {

// add one '0' after 5 consecutive 1's


s = s + ch + '0';
cnt = 0;
}
}
else {
s += ch;
cnt = 0;
}
}

// add flag byte in the beginning


// and end of stuffed data
s = "01111110" + s + "01111110";

System.out.println("Data stuffed in client: " + s);


System.out.println("Sending to server for unstuffing");
dos.writeUTF(s);
}
}

You might also like