-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoBackN_Non_NACK.c
67 lines (51 loc) · 1.58 KB
/
goBackN_Non_NACK.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// return whether the current frame has been damaged or lost.
int damaged_lost()
{
return (rand()%5 == 0); //a frame is lost or damaged with probability of 20%.
}
int min(int a, int b)
{
return (a < b)? a : b;
}
int main()
{
srand(time(NULL));
int n_frames, N; // N = sliding window size
printf("Enter the number of frames: ");
scanf("%d", &n_frames);
printf("Enter the sliding window size: ");
scanf("%d", &N);
int i = 1; // stores the frame to be sent next.
int problem_occurred = 0; // a flag to indicate whether there was a lost or damaged frame.
printf("\n");
while(i <= n_frames)
{
//sender
printf("Frames sent: ");
for(int j = i; j < min(i+N, n_frames+1); j++)
printf("%d ", j);
printf("\n");
//receiver
problem_occurred = 0;
for(int j = i; j < min(i+N, n_frames+1); j++)
{
if(damaged_lost()) //if the current frame is damaged or lost
{
problem_occurred = 1;
break;
}
}
//sender again
if(!problem_occurred) //if everything went fine
{
printf("Acknowledgment received for Frames %d to %d\n\n", i, min(i+N-1, n_frames));
i += N;
continue;
}
//if a frame was lost or damaged, we have to send all the frames in the current window again.
printf("No acknowledgement received\n\nSending all the frames in the current window again\n");
}
}