0% found this document useful (0 votes)
32 views4 pages

Program 5

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

Program 5

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

Program- 5

Write a program to implement sliding window protocol in datalink


layer.

The sliding window protocol is a method used in network communications to manage the flow of
data between two devices and ensure that data is transmitted efficiently and accurately. It's
particularly useful in scenarios where data is sent over a network with potential delays or errors.
Here’s a breakdown of how it works:

1. Window Size: Both sender and receiver maintain a "window" of frames or packets that can be
sent or received. This window size determines the maximum number of frames that can be sent
without receiving an acknowledgment.
2. Sender's Window: The sender can transmit a number of frames up to the window size before
needing to wait for an acknowledgment from the receiver. Once the sender receives an
acknowledgment for some of the frames, it can slide the window forward and send new
frames.
3. Receiver's Window: The receiver also has a window size that specifies the number of frames it
can buffer. It keeps track of which frames it has received correctly and sends acknowledgments
back to the sender. If the receiver’s buffer is full, it will need to wait before it can accept more
frames.
4. Sliding the Window: As acknowledgments are received, both the sender and receiver "slide"
their windows forward. This sliding process means that as old frames are acknowledged and
removed from the window, new frames can be sent or received.
5. Error Handling: The protocol includes mechanisms for handling errors. If a frame is lost or
corrupted, the receiver may request retransmission of that frame, and the sender will retransmit
it, usually sliding the window back to the point where the error occurred.
6. Flow Control: The sliding window protocol helps with flow control by preventing the sender from
overwhelming the receiver with too many frames at once. The receiver's window size ensures that
it only processes a manageable number of frames at any time.

This protocol is commonly used in various data link layer and transport layer protocols, such
as the Transmission Control Protocol (TCP) in the Internet Protocol Suite.
Source code:
import [Link];
import [Link];
public class SlidingWindowProtocol {

private static final int WINDOW_SIZE = 4; // Size of the sliding window


private static final int TOTAL_PACKETS = 10; // Total number of packets to send

// Simulates the sender's sliding window protocol


public static void sender() {
Queue<Integer> window = new LinkedList<>();
for (int i = 0; i < WINDOW_SIZE; i++) {
if (i < TOTAL_PACKETS) {
[Link](i);
[Link]("Sent packet: " + i);
}
}

int nextPacketToSend = WINDOW_SIZE;

while (nextPacketToSend < TOTAL_PACKETS || ![Link]()) {


if (![Link]()) {
// Simulate receiving acknowledgment for the packet at the start of the window
int ack = [Link]();
[Link]("Acknowledgment received for packet: " + ack);
}

// Slide the window


if (nextPacketToSend < TOTAL_PACKETS)
{ [Link](nextPacketToSend);
[Link]("Sent packet: " + nextPacketToSend);
nextPacketToSend++;
}

// Simulate a delay for demonstration purposes


try {
[Link](1000); // 1 second delay
} catch (InterruptedException e) {
[Link]();
}
}

[Link]("All packets sent and acknowledged.");


}

public static void main(String[] args) {


sender();
}

}
Explanation

1. Initialization:
• The window size (WINDOW_SIZE) determines how many
packets can be sent before receiving acknowledgments.
• TOTAL_PACKETS represents the total number of packets to send.
2. Sender Function:
• Initializes the window with the first set of packets.
• Simulates sending packets and receiving acknowledgments.
• Slides the window by removing the acknowledged packet and adding a
new packet.
3. Sliding the Window:
• After sending packets, the window slides by removing the packet
at the start of the window (simulating acknowledgment) and
adding new packets as they are sent.
4. Simulation:
• The [Link](1000) call simulates a delay,
representing the time between sending packets and
receiving acknowledgments.
OUTPUT

Sent packet: 0
Sent packet: 1
Sent packet: 2
Sent packet: 3
Acknowledgm
ent received
for packet: 0
Sent packet: 4
Acknowledgm
ent received
for packet: 1
Sent packet: 5
Acknowledgm
ent received
for packet: 2
Sent packet: 6
Acknowledgm
ent received
for packet: 3
Sent packet: 7
Acknowledgm
ent received
for packet: 4
Sent packet: 8
Acknowledgm
ent received
for packet: 5
Sent packet: 9
Acknowledgm
ent received
for packet: 6
Acknowledgm
ent received
for packet: 7
Acknowledgm
ent received
for packet: 8
Acknowledgm
ent received
for packet: 9
All packets
sent and
acknowledged.

You might also like