-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStopAndWait.c
More file actions
109 lines (88 loc) · 2.8 KB
/
StopAndWait.c
File metadata and controls
109 lines (88 loc) · 2.8 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>
struct frame {
int info;
int seq;
};
int ak;
int t = 5, k;
int disconnect = 0;
struct frame p;
char turn = 's'; // Initialize first turn as sender
int errorframe = 1; // no Error
int errorack = 1;
void sender();
void receiver();
void main() {
p.info = 0; // data part
p.seq = 0; // sequence number
while (!disconnect) {
sender(); // call sender
for (k = 1; k <= 10000000; k++)
;
// After a finite amount of time call receiver
receiver();
}
}
void sender() {
static int flag = 0;
if (turn == 's') { // sender turn
if (errorack == 0) { // Ack didn't arrive
printf("SENDER: sent packet with seq NO:%d\n", p.seq);
errorframe = rand() % 4; // randomly pick Error frame as 4
printf("%s\n", (errorframe == 0 ? "Error While sending Packet" : ""));
turn = 'r';
} else {
if (flag == 1)
printf("SENDER: Received ACK for packet %d\n", ak);
if (p.seq == 5) {
disconnect = 1;
return;
}
p.info = p.info + 1;
p.seq = p.seq + 1;
printf("SENDER: sent packet with seq NO:%d\n", p.seq);
errorframe = rand() % 4;
printf("%s\n", (errorframe == 0 ? "Error While sending Packet" : ""));
turn = 'r';
flag = 1;
}
} else {
t--;
printf("SENDER time reducing\n");
if (t == 0) {
turn = 's';
errorack = 0;
t = 5;
}
}
}
void receiver() {
static int frexp = 1;
if (turn == 'r') {
if (errorframe != 0) {
if (p.seq == frexp) { // if frame sequence number is eq to frexp
printf("RECEIVER: Received packet with seq %d\n", p.seq);
// note sequence number of frame arrived
// to send acknowledgement
ak = p.seq;
// increment the frame sequence number
frexp = frexp + 1;
// Set next turn as sender
turn = 's';
// Send acknowledgement error for frame number 4
errorack = rand() % 4;
printf("%s\n", (errorack == 0 ? "Error While sending ACK" : ""));
} else {
// Receiver received Duplicated frame for lost frame after Resending
printf("RECEIVER: Duplicated packet with seq %d\n", frexp - 1);
// Note down acknowledgement number of frame
ak = frexp - 1;
// next turn sender
turn = 's';
errorack = rand() % 4;
printf("%s\n", (errorack == 0 ? "Error While sending ACK" : ""));
}
}
}
}