Name: Swati Ananda Sanap
PRN: 2425010048
Roll No: 67
Div: SY C
Experiment no.5
• Flow Control Mechanism:
1. Stop and Wait ARQ:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main()
{
int packet_number, num_packets;
srand(time(NULL));
printf("Enter the number of packets to send: ");
scanf("%d", &num_packets);
if (num_packets <= 0)
{
printf("Please enter a positive number.\n");
return 1;
}
for (packet_number = 1; packet_number <= num_packets; packet_number++)
{
int ack_received = 0;
while (!ack_received)
{
printf("Sending packet %d...\n", packet_number);
sleep(1);
if (rand() % 2 == 0)
{
printf("Received ACK for packet %d\n", packet_number);
ack_received = 1;
}
else
{
printf("ACK for packet %d lost, resending...\n", packet_number);
sleep(1);
}
}
}
printf("All packets sent successfully!\n");
return 0;
}
Output:
2. Go Back N ARQ:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int num_packets, WINDOW_SIZE;
printf("Enter the number of packets to send: ");
scanf("%d", &num_packets);
if (num_packets <= 0)
{
printf("Please enter a positive number.\n");
return 1;
}
printf("Enter the window size: ");
scanf("%d", &WINDOW_SIZE);
if (WINDOW_SIZE <= 0)
{
printf("Please enter a positive window size.\n");
return 1;
}
int base = 0;
while (base < num_packets)
{
for (int i = base; i < base + WINDOW_SIZE && i < num_packets; i++)
{
printf("Sending packet %d...\n", i);
sleep(1);
}
for (int i = base; i < base + WINDOW_SIZE && i < num_packets; i++)
{
if (rand() % 2 == 0)
{
printf("Received ACK for packet %d\n", i);
base++;
}
else
{
printf("ACK for packet %d lost\n", i);
printf("Resending packets from %d to %d\n", base, base + WINDOW_SIZE - 1);
break;
}
}
}
printf("All packets sent successfully!\n");
return 0;
Output:
3. Selective Repeat ARQ:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_PACKETS 10
int main()
{
int num_packets, WINDOW_SIZE;
printf("Enter the number of packets to send (max %d): ", MAX_PACKETS);
scanf("%d", &num_packets);
if (num_packets <= 0 || num_packets > MAX_PACKETS)
{
printf("Please enter a positive number within the limit.\n");
return 1;
}
printf("Enter the window size: ");
scanf("%d", &WINDOW_SIZE);
if (WINDOW_SIZE <= 0 || WINDOW_SIZE > num_packets)
{
printf("Please enter a positive window size not exceeding the number of packets.\n");
return 1;
}
int base = 0;
int acked[MAX_PACKETS] = {0};
while (base < num_packets) {
for (int i = base; i < base + WINDOW_SIZE && i < num_packets; i++)
{
if (!acked[i])
{
printf("Sending packet %d...\n", i);
sleep(1);
}
}
for (int i = base; i < base + WINDOW_SIZE && i < num_packets; i++)
{
if (rand() % 2 == 0)
{
printf("Received ACK for packet %d\n", i);
acked[i] = 1;
}
else
{
printf("ACK for packet %d lost\n", i);
}
}
while (acked[base] && base < num_packets) {
base++;
}
}
printf("All packets sent successfully!\n");
return 0;
}
Output:
Theory:
Flow control is a synchronization protocol for transmitting and receiving units of
information. It determines the advance of information between a sender and a receiver,
enabling and disabling the transmission of information.
Flow control is design issue at Data Link Layer. It is a technique that generally
observes the proper flow of data from sender to receiver. It is very essential because it
is possible for sender to transmit data or information at very fast rate and hence
receiver can receive this information and process it. This can happen only if receiver
has very high load of traffic as compared to sender, or if receiver has power of
processing less as compared to sender. Flow control is basically a technique that gives
permission to two of stations that are working and processing at different speeds to
just communicate with one another. Flow control in Data Link Layer simply restricts
and coordinates number of frames or amount of data sender can send just before it
waits for an acknowledgement from receiver. Flow control is actually set of
procedures that explains sender about how much data or frames it can transfer or
transmit before data overwhelms receiver. The receiving device also contains only
limited amount of speed and memory to store data. This is why receiving device
should be able to tell or inform the sender about stopping the transmission or
transferring of data on temporary basis before it reaches limit. It also needs buffer,
large block of memory for just storing data or frames until they are processed.
Flow Control Protocols:
1. Stop and Wait ARQ
Sender:
Rule 1) Send one data packet at a time.
Rule 2) Send the next packet only after receiving acknowledgement for the previous.
Receiver:
Rule 1) Send acknowledgement after receiving and consuming a data packet.
Rule 2) After consuming packet acknowledgement need to be sent (Flow Control)
2. Go Back N
Go-Back-N automatic repeat request is a flow control protocol)where the sender
continues to send several frames specified by a window size even without receiving
feedback from the receiver node. It can be said that it's a special case of the general
sliding window protocol where the transmitter or sender window size is N and the
receiver's window size is 1, which means that it can transmit N frames to the receiving
node before waiting for feedback.
Now the receiver's duty is to keep track of the sequence number of the next frame it
expects to receive and sends the feedback after every data packet it receives. Once the
sender has sent all the frames in its window, it makes sure that it has then received all the
feedback of the transmitted data packets, if in any case, it hasn't received feedback of any
data packet and the time-out timer expires, it then resends all the data packets again,
starting from the lost data packet to the final data packet.
Sender window size (WS):
In Go-Back-N ARQ Protocol, N is the sender window size, which we can see in the
above example was 5. Now, here N should be greater than 1 in order to implement
pipelining. If N=1, then our system reduces to the Stop & Wait protocol.
Now the efficiency of Go-Back-N ARQ = N/(1+2a), where a = tp/tt.
Where tp is the propagation delay and tt is the transmission delay
Also, tt = D/B; and here D = data size and B = bandwidth
And tp = d/s, here d = distance and s = wave propagation speed.
Now to find the effective bandwidth (or throughput),
Effective bandwidth = efficiency * bandwidth, which means,
Effective bandwidth = (N/(1+2a)) *B
Receiver window size (WR):
WR is always equal to 1 in the case of Go-Back-N ARQ.
4. Selective Repeat
Selective Repeat Protocol (SRP) is a type of error control protocol we use in computer
networks to ensure the reliable delivery of data packets. Additionally, we use it in
conjunction with the Transmission Control Protocol (TCP) to ensure that the receiver
receives data transmitted over the network without errors.
In the SRP, the sender divides the data into packets and sends them to the receiver.
Furthermore, the receiver sends an acknowledgment (ACK) for each packet received
successfully. If the sender doesn’t receive an ACK for a particular packet, it
retransmits only that packet instead of the entire set of packets.
The SRP uses a window-based flow control mechanism to ensure the sender doesn’t
overwhelm the receiver with too many packets. Additionally, the sender and receiver
maintain a window of packets. Based on the window size, the sender sends packets
and waits for a specific amount of time for acknowledgment from the receiver