-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteStuffingServer.java
More file actions
45 lines (38 loc) · 1.6 KB
/
ByteStuffingServer.java
File metadata and controls
45 lines (38 loc) · 1.6 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
45
import java.io.*;
import java.net.*;
class ByteStuffingServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(6789);
System.out.println("Server is running... Waiting for client...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String stuffedData = inFromClient.readLine();
System.out.println("Received Stuffed Data: " + stuffedData);
// Perform byte unstuffing
String unstuffedData = byteUnstuffing(stuffedData);
System.out.println("Original Data After Unstuffing: " + unstuffedData);
inFromClient.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String byteUnstuffing(String stuffedData) {
StringBuilder result = new StringBuilder();
boolean escapeNext = false;
for (char c : stuffedData.toCharArray()) {
if (escapeNext) {
result.append(c); // Add the escaped character as it is
escapeNext = false;
} else if (c == 'E') {
escapeNext = true; // Escape detected, next character is stuffed
} else {
result.append(c); // Normal character
}
}
return result.toString();
}
}