Sliding Window Protocols
Sliding window protocols are a class of data link layer protocols that allow for more efficient data transmission by enabling multiple frames to be sent before requiring an acknowledgment for the first one. This approach improves throughput and reduces the idle time that can occur in simpler protocols like stop-and-wait.
Key Concepts of Sliding Window Protocols
1.Window Size: The “window” refers to the number of frames that can be sent without receiving an acknowledgment. The size of the window determines how many frames can be in transit at any given time. For instance, a window size of 4 means that the sender can send 4 frames before needing to wait for an acknowledgment.
2.Sequence Numbers: Each frame is assigned a unique sequence number, which helps the receiver identify frames and manage acknowledgments. The sequence numbers are typically modulo-n, where n is the window size. This allows for efficient tracking of frames in transit.
3.Acknowledgments: The receiver sends back acknowledgments for frames that it has successfully received. Depending on the protocol, these acknowledgments can be cumulative (acknowledging all frames up to a certain point) or individual (acknowledging each frame separately).
4.Sliding Mechanism: As the sender receives acknowledgments from the receiver, the window “slides” forward, allowing the sender to transmit new frames while keeping track of the frames that have been acknowledged. This sliding mechanism enables continuous transmission of frames, maximizing the use of the available bandwidth.
A sliding window of size 1, with a 3-bit sequence number. (a) Ini tially. (b) After the first frame has been sent. (c) After the first frame has been received. (d) After the first acknowledgement has been received
Types of Sliding Window Protocols
1.Go-Back-N Protocol: In this protocol, the sender can send multiple frames (up to the window size) but must wait for an acknowledgment for the first unacknowledged frame before sending more frames. If a frame is lost or damaged, the sender must retransmit that frame and all subsequent frames, which can lead to inefficiencies.
Go-Back-N Protocol Code Example:
#include <stdio.h>
#define WINDOW_SIZE 4
#define MAX_FRAMES 10
typedef enum { FRAME_ARRIVAL, TIMEOUT, ACK_RECEIVED } event_type;
void send_frame(int frame) {
printf("Sent frame %d\n", frame);
}
void start_timer(int frame) {
printf("Started timer for frame %d\n", frame);
}
void wait_for_event(event_type* event) {
*event = ACK_RECEIVED;
}
void sender() {
int next_frame_to_send = 0;
int base = 0;
event_type event;
while (base < MAX_FRAMES) {
while (next_frame_to_send < base + WINDOW_SIZE && next_frame_to_send < MAX_FRAMES) {
send_frame(next_frame_to_send);
start_timer(next_frame_to_send);
next_frame_to_send++;
}
wait_for_event(&event);
if (event == ACK_RECEIVED) {
base++;
} else if (event == TIMEOUT) {
for (int i = base; i < next_frame_to_send; i++) {
send_frame(i);
start_timer(i);
}
}
}
}
int receive_frame() {
return 0;
}
void deliver_to_network_layer(int frame) {
printf("Delivered frame %d to network layer\n", frame);
}
void send_ack(int frame) {
printf("Sent ACK for frame %d\n", frame);
}
void receiver() {
int expected_frame = 0;
int received_frame;
while (expected_frame < MAX_FRAMES) {
received_frame = receive_frame();
if (received_frame == expected_frame) {
deliver_to_network_layer(received_frame);
send_ack(received_frame);
expected_frame++;
} else {
send_ack(expected_frame - 1);
}
}
}
int main() {
sender();
receiver();
return 0;
}
2.Selective Repeat Protocol: This protocol allows the sender to retransmit only the specific frames that were lost or damaged, rather than all subsequent frames. The receiver maintains a buffer to store out-of-order frames until the missing frames are received. This approach is more efficient than Go-Back-N, as it minimizes the number of retransmissions.
Selective Repeat Protocol Code Example:
#include <stdio.h>
#define WINDOW_SIZE 4
#define MAX_FRAMES 10
// Enum to represent events
typedef enum { FRAME_ARRIVAL, TIMEOUT, ACK_RECEIVED } event_type;
// Simplified sender function
void sender() {
int next_frame_to_send = 0;
int base = 0;
event_type event;
int acked[MAX_FRAMES] = {0};
while (base < MAX_FRAMES) {
// Send frames while within the window
while (next_frame_to_send < base + WINDOW_SIZE && next_frame_to_send < MAX_FRAMES) {
printf("Sending frame %d", next_frame_to_send);
next_frame_to_send++;
}
// Wait for event (simulated as ACK_RECEIVED here)
event = ACK_RECEIVED;
if (event == ACK_RECEIVED) {
acked[base] = 1; // Mark the frame as acknowledged
base++; // Move the base forward
// Reset acknowledged frames
while (acked[base] == 1) {
acked[base] = 0;
base++;
}
} else if (event == TIMEOUT) {
// Resend unacknowledged frames
for (int i = base; i < next_frame_to_send; i++) {
if (!acked[i]) {
printf("Resending frame %d", i);
}
}
}
}
}
// Simplified receiver function
void receiver() {
int expected_frame = 0;
int received_frame;
while (expected_frame < MAX_FRAMES) {
// Simulate receiving a frame (just incrementing here)
received_frame = expected_frame;
if (received_frame == expected_frame) {
printf("Received frame %d", received_frame);
expected_frame++;
} else {
printf("Frame out of order, sending ACK for %d", expected_frame - 1);
}
}
}
int main() {
sender();
receiver();
return 0;
}
Conclusion
These examples illustrate how sliding window protocols can be implemented in a coding structure. The actual implementation would depend on the specific programming language and the environment in which the protocol is being used. The key concepts of window management, frame transmission, and acknowledgment handling remain consistent across variations