5.
Develop a program to implement sliding window protocol in data link layer
import java.util.Scanner;
public class SlidingWindowProtocolDataLink {
static void slidingWindowProtocol(int totalFrames, int windowSize) {
Scanner scanner = new Scanner(System.in);
int sent = 0; // Tracks the starting frame of the current window
while (sent < totalFrames) {
// Transmit all frames in the current window
System.out.println("\n--- Sending Frames in Current Window ---");
for (int i = 0; i < windowSize && (sent + i) < totalFrames; i++) {
System.out.println("Frame " + (sent + i) + " sent.");
// Simulating acknowledgment from receiver
System.out.print("Enter the last successfully received frame (-1 for NACK or
timeout): ");
int ack = scanner.nextInt();
if (ack == -1) {
// If no acknowledgment, retransmit the current window
System.out.println("Acknowledgment failed! Retransmitting the current
window...");
} else if (ack >= sent && ack < sent + windowSize) {
// Slide the window to the frame after the last acknowledged frame
sent = ack + 1;
System.out.println("Acknowledgment received for Frame " + ack + ". Sliding
window...");
} else {
// Invalid acknowledgment input
System.out.println("Invalid acknowledgment input! Retransmitting...");
System.out.println("\nAll frames sent and acknowledged successfully!");
scanner.close();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the total number of frames and window size
System.out.print("Enter the total number of frames to send: ");
int totalFrames = scanner.nextInt();
System.out.print("Enter the sliding window size: ");
int windowSize = scanner.nextInt();
// Call the sliding window protocol simulation
slidingWindowProtocol(totalFrames, windowSize);
scanner.close();