-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGo_back_N.c
50 lines (42 loc) · 1.31 KB
/
Go_back_N.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<unistd.h>// for sleep function i.e sleep()
bool send_packt(int packt){
if(rand()%10 < 2){
printf("Packet %d lost during transmission\n", packt);
return false;
}
printf("Packet %d sent successfully\n", packt);
return true;
}
int main(){
int window_size, total_packts;
printf("Enter the window size: ");
scanf("%d", &window_size);
printf("Enter the totla number of packets: ");
scanf("%d", &total_packts);
int base=0;
int nxt_sq_no=0;
srand(time(NULL));
while(base<total_packts){
while(nxt_sq_no < base+window_size && nxt_sq_no<total_packts){
if(send_packt(nxt_sq_no)){
printf("Waiting for the ack of the packet %d \n", nxt_sq_no);
}
nxt_sq_no++;
}
int ack=base + (rand()%(nxt_sq_no-base+1));//random ack generation
printf("Ack recieved from packet %d\n", ack);
if(ack>=base){
base=ack+1;//slide the window
}
else{
printf("Ack for packet %d not recieved, retransmitting from packet %d\n", base, base);
nxt_sq_no=base;//retranmit the base
}
sleep(1);//to simulate delay
}
printf("All packets successfully sent\n");
return 0;
}