-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteStuffingClient.java
More file actions
44 lines (35 loc) · 1.4 KB
/
ByteStuffingClient.java
File metadata and controls
44 lines (35 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.*;
import java.net.*;
import java.util.Scanner;
class ByteStuffingClient {
public static void main(String[] args) {
try {
Socket clientSocket = new Socket("localhost", 6789);
System.out.println("Connected to Server!");
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sequence of D (Data), E (Escape), F (Flag): ");
String inputData = scanner.nextLine();
// Perform byte stuffing
String stuffedData = byteStuffing(inputData);
System.out.println("Stuffed Data Sent: " + stuffedData);
// Send stuffed data
outToServer.writeBytes(stuffedData + "\n");
scanner.close();
outToServer.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String byteStuffing(String data) {
StringBuilder stuffed = new StringBuilder();
for (char c : data.toCharArray()) {
if (c == 'F' || c == 'E') {
stuffed.append("E"); // Add escape before special characters
}
stuffed.append(c); // Add actual character
}
return stuffed.toString();
}
}