while (true) { System.out.println("Enter the Message to be Sent : "); Stringdata= sc.nextLine(); Stringres=newString();
// Data in each frame is stuffed by 'F' at beginning and end data = 'F' + data + 'F'; for (inti=0; i < data.length(); i++) {
// Stuff with 'E' if 'F' is found in the data to be sent if (data.charAt(i) == 'F' && i != 0 && i != (data.length() - 1)) res = res + 'E' + data.charAt(i);
// Stuff with 'E' if 'E' is found in the data to be sent elseif (data.charAt(i) == 'E') res = res + 'E' + data.charAt(i); else res = res + data.charAt(i); }
System.out.println("The data being sent (with byte stuffed) is : " + res);
// Send the data to the receiver dos.writeUTF(res); System.out.println("Seding Message...."); if (dis.readUTF().equals("success")) System.out.println("Thanks for the Feedback Server!!");
// End Messaging dos.writeUTF("bye"); break; }
// Close all connections s.close(); dis.close(); dos.close(); } }
while (true) { Stringout=newString(); // Used to read the data sent by client Stringres= dis.readUTF(); System.out.println("Message Received...Successfully!!!"); System.out.println("The Stuffed Message is : " + res);
for (inti=1; i < res.length() - 1; i++) {
// If data contains a 'D' or 'F' do not unstuff it if (res.charAt(i) == 'D' || res.charAt(i) == 'F') out = out + res.charAt(i);
// If data contains 'E' followed by 'E', de-stuff the former 'E' elseif (res.charAt(i) == 'E' && res.charAt(i + 1) == 'E') { out = out + 'E'; i++; } } System.out.println("The Destuffed Message is : " + out); dos.writeUTF("success"); Stringch= dis.readUTF(); if (ch.equals("bye")) { System.out.println("Messaging is over.....EXITING"); break; } }
// Closing all connections socket.close(); dis.close(); dos.close(); } }