📅  最后修改于: 2023-12-03 15:15:21.475000             🧑  作者: Mango
Go-Back-N Automatic Repeat Request (ARQ) is a type of error control protocol used in data transmission. It is a sliding window protocol that is used in the data link layer of OSI Model.
Here is a simple implementation of Go-Back-N ARQ in Python:
from time import time, sleep
import random
def sender():
timeout = 2
seq_num_bits = 3
seq_limit = 2 ** seq_num_bits
window_size = 4
send_buffer = list(range(0, seq_limit))
send_base = 0
next_frame_to_send = 0
while send_base < seq_limit:
if (next_frame_to_send < send_base + window_size) and (next_frame_to_send < seq_limit):
print("Sending frame", next_frame_to_send)
sleep(random.uniform(0.1, 0.2))
if random.randint(0, 1) == 1:
print("ACK not received for frame", next_frame_to_send)
continue
if next_frame_to_send == send_base:
start_timer = time()
next_frame_to_send += 1
if (time() - start_timer) > timeout:
print("Timeout occurred for frame", send_base)
next_frame_to_send = send_base
if next_frame_to_send == (send_base + window_size) or next_frame_to_send == seq_limit:
print("ACK received for all frames up to", next_frame_to_send)
send_base = next_frame_to_send
def receiver():
seq_num_bits = 3
seq_limit = 2 ** seq_num_bits
expected_frame = 0
while expected_frame < seq_limit:
if random.randint(0, 1) == 1:
print("Frame lost for sequence number", expected_frame)
continue
print("Frame received with sequence number", expected_frame)
sleep(random.uniform(0.1, 0.2))
expected_frame += 1
print("All frames received successfully.")
if __name__ == '__main__':
sender()
receiver()
This implementation demonstrates how the sender sends frames to the receiver in a sliding window approach. The sender waits for ACK for a specific time, and if not received, it retransmits all the unACKnowledged frames beginning from the last frame ACKnowledged by the receiver. The receiver sends cumulative ACK, acknowledging receipt of all frames up to the received frame's sequence number. If a frame is lost or damaged, the receiver discards it and sends a NAK specifying the expected sequence number. The sender then retransmits all the frames from the expected sequence number.
Go-Back-N ARQ is a reliable error control protocol that allows efficient flow control and ensures data transmission in a sliding window approach. Its features make it one of the most popular protocols used in data transmission today.