Exp.
No:4
Implementation of Go-back N and Selective Repeat Protocol
Date:19/03/2025
Aim
To study and implement Go-back N and Selective Repeat Protocols using C.
Software Required
Turbo C/Dev-C++
i) Go-back
N Algorithm
Step1: Start the program.
Step2: Initialize the required variables:
● Window to store the sliding window size.
● f to store the total number of frames to be transmitted.
● frames [50] to store the frame values.
● back to store the last acknowledgment received from the receiver.
Step3: Get user input:
● Prompt the user to enter the window size and store the value.
● Prompt the user to enter the total number of frames to be transmitted and
store the value.
● Prompt the user to enter the frame values and store them in the frames array.
Step4: Display information about the Go-Back-N protocol:
● Print a message stating that the frames are sent using the Sliding
Window Protocol with Go-Back-N ARQ.
● Explain that the sender sends frames in batches equal to the window size
and waits for acknowledgment from the receiver.
Step 5: Send frames in batches using the sliding window technique:
● Loop through each frame:
● Print the frame number being sent.
● If a full window of frames has been sent, print a message that
an acknowledgment for these frames has been received.
● If the total number of frames is not a perfect multiple of the window size,
print an acknowledgment for the remaining frames.
Step6: Simulate a lost acknowledgment scenario:
● Ask the user to enter the last received acknowledgment number.
Output
Enter window size: 4
Enter number of frames to transmit: 13
Enter 14 frames: 1
2
3
4
5
6
7
8
9
10
11
12
13
go back n With sliding window protocol the frames are se
After sending 4 frames at each stage sender waits for acknowledgement sent by the receiver
1234
Acknowledgement of above frames sent is received by sender
5678
Acknowledgement of above frames sent is received by sender
9 10 11 12
Acknowledgement of above frames sent is received by sender
13
Acknowledgement of above frames sent is received by sender
Please enter the last Acknowledgement received.
12
Frame 12 has been transmitted.
Frame 13 has been transmitted.
● Retransmit all frames starting from the last acknowledged frame until the last frame
is successfully sent.
Step7: End the program.
Program
#include<stdio.h>
int main()
{
int
window,i,f,frames[50],ack;
printf("Enter window size:
"); scanf("%d",&window);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\n go back n \n\n");
printf("\nWith sliding window protocol the frames are sent \n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by
the receiver\n\n",window);
for(i=1;i<=f;i++)
{
if(i%window==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%window!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);
for(i=ack;i<=f;i++){
printf("Frame %d has been transmitted.\n",i);
}
ii) Selective Repeat
protocol Algorithm
Step1: Start the process.
Step2: Initialize variables:
● Declare a variable to store the window size.
● Declare a variable to store the number of frames.
● Create an array to store the frames.
● Declare a variable to store the last received acknowledgment.
Step3: Accept user inputs:
● Ask the user to enter the window size.
● Ask the user to enter the number of frames to be transmitted.
● Take input for each frame and store it in an array.
Step4: Display the transmission process:
● Print a message indicating that the sliding window protocol is being used.
● Mention that after sending window frames, the sender waits for acknowledgment.
Step5: Transmit frames using the sliding window technique:
● Loop through each frame from the first to the last:
● If the frame index is a multiple of the window size, print the frame and indicate
that acknowledgment has been received.
● Otherwise, print the frame without waiting for acknowledgment.
Step6: Handle any remaining frames:
● If the total number of frames is not a multiple of the window size, print a message
that acknowledgment has been received for the remaining frames.
Step7: Process acknowledgment from the receiver:
● Ask the user to enter the last acknowledgment received.
● Print a message confirming that the respective frame has been transmitted.
Step8: End the process.
Output
Enter window size: 3
Enter number of frames to transmit: 133
Enter 13 frames: 1
2
3
4
5
6
7
8
9
10
11
12
13
Selective repeat
With sliding window protocol the frames are sent
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
123
Acknowledgement of above frames sent is received by sender
456
Acknowledgement of above frames sent is received by sender
789
Acknowledgement of above frames sent is received by sender
10 11 12
Acknowledgement of above frames sent is received by sender
13
Acknowledgement of above frames sent is received by sender
Enter the last Acknowledgement received.
6
Frame 6 has been transmitted.
Program
#include<stdio.h>
int main()
{
int window,i,f,frames[50],ack;
printf("Enter window size: ");
scanf("%d",&window);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\n Selective repeat \n\n");
printf("\nWith sliding window protocol the frames are sent \n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by
the receiver\n\n",window);
for(i=1;i<=f;i++)
{
if(i%window==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%window!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n"); printf("\
nEnter the last Acknowledgement received.\n");
scanf("%d",&ack);
printf("Frame %d has been transmitted.\n",ack);
}
Inference
Go-back N: The frames are sent to the receiver continuously, the sender asks for the
last acknowledgement, from the acknowledgement received the sender transmits all the
frames after the last acknowledgement received.
Selective Repeat Protocols: The frames are sent to the receiver continuously, the sender
asks for the last acknowledgement. The sender will send only the frames for which the
acknowledgement is received.
Result
Thus, sliding window Go Back N and Selective Repeat protocols are implemented
using C.