-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteStuffingClientUDP.java
More file actions
46 lines (35 loc) · 1.46 KB
/
ByteStuffingClientUDP.java
File metadata and controls
46 lines (35 loc) · 1.46 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
46
import java.io.*;
import java.net.*;
import java.util.Scanner;
class ByteStuffingClientUDP {
public static void main(String[] args) {
try {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
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
byte[] sendBuffer = stuffedData.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, serverAddress, 6789);
clientSocket.send(sendPacket);
scanner.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();
}
}