Implementation of Selective Repeat ARQ Protocol Using UDP
To implement sliding window protocol with Selective Repeat ARQ protocol using socket programming in C.
Algorithms
Algorithm: Client (Sliding Window Sender – Selective Repeat)
Step 1: Create UDP Socket
- Create a UDP socket using
socket(AF_INET, SOCK_DGRAM, 0). - If socket creation fails, terminate the program.
Step 2: Define Server Address
- Set address family to
AF_INET. - Set port number to
5555. - Set IP address to
127.0.0.1.
Step 3: Initialize Variables
-
Define:
PACKET_COUNT= total number of packets.WINDOW_SIZE= sliding window size.
-
Initialize:
acked[PACKET_COUNT] = 0(to track acknowledgements).base = 0(start of the window).next = 0(next packet to send).
Step 4: Start Transmission Loop
- Repeat while
base < PACKET_COUNT:
Step 4.1: Send Packets Within Window
-
While:
next < base + WINDOW_SIZE, andnext < PACKET_COUNT
-
Do:
- Create a packet containing sequence number
(next + 1). - Send the packet to the server using
sendto(). - Increment
next.
- Create a packet containing sequence number
Step 4.2: Receive Acknowledgement
- Wait for an ACK from the server using
recvfrom(). - Extract the acknowledged packet number from the received message.
Step 4.3: Mark Packet as Acknowledged
- Set
acked[ack_number - 1] = 1.
Step 4.4: Slide the Window
-
While:
base < PACKET_COUNTandacked[base] == 1
-
Increment
baseto move the window forward.
Step 5: Terminate Client
- Close the socket after all packets are acknowledged.
- End the program.
Algorithm: Server (Receiver with Individual ACKs)
Step 1: Create UDP Socket
- Create a UDP socket using
socket(AF_INET, SOCK_DGRAM, 0). - If socket creation fails, terminate the program.
Step 2: Define Server Address
- Set address family to
AF_INET. - Set port number to
5555. - Set IP address to
127.0.0.1.
Step 3: Bind Socket
- Bind the socket to the specified IP address and port using
bind(). - If binding fails, terminate the program.
Step 4: Initialize Data Structures
- Create an array
received[PACKET_COUNT]initialized to0to track received packets.
Step 5: Start Receiving Loop
- Repeat indefinitely:
Step 5.1: Receive Packet
- Receive a packet from the client using
recvfrom(). - Extract the packet sequence number.
Step 5.2: Validate Packet Number
-
If the packet number is outside the valid range:
- Discard the packet.
- Continue waiting for the next packet.
Step 5.3: Process Packet
-
If the packet has not been received before:
- Mark it as received.
- Print confirmation of reception.
-
Else:
- Identify it as a duplicate packet.
Step 5.4: Send Acknowledgement
- Send an acknowledgement in the format
"ACK <packet_number>"back to the client usingsendto().
Step 6: Continue Operation
- Continue receiving packets and sending ACKs indefinitely.