0% found this document useful (0 votes)
4 views2 pages

6

The document presents a Java program that implements the sliding window protocol in the data link layer. It allows the user to input the total number of frames and the window size, then simulates sending frames and receiving acknowledgments. The program handles acknowledgment failures and invalid inputs by retransmitting the current window as necessary.

Uploaded by

kavichengappa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

6

The document presents a Java program that implements the sliding window protocol in the data link layer. It allows the user to input the total number of frames and the window size, then simulates sending frames and receiving acknowledgments. The program handles acknowledgment failures and invalid inputs by retransmitting the current window as necessary.

Uploaded by

kavichengappa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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();

You might also like