-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiver.c
More file actions
33 lines (27 loc) · 1.34 KB
/
Receiver.c
File metadata and controls
33 lines (27 loc) · 1.34 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
/**
* This code snippet demonstrates a simple receiver function for a communication protocol,
* likely related to some form of Automatic Repeat reQuest (ARQ) method. The receiver simulates the reception of frames,
* checking for corruption based on an error probability, and sends acknowledgments for the received frames.
**/
#include <stdio.h>
#include <stdlib.h>
#include "Frame.h"
// Determine if the frame is corrupted based on the error probability
bool isCorrupted(double errorProb) {
return ((double) rand() / RAND_MAX) < errorProb;
}
void receiver(double errorProb) {
Frame receivedFrame;
while (1) {
receivedFrame.seqNum = rand() % 2; // Generate a random sequence number (0 or 1)
if (isCorrupted(errorProb)) {
printf("---> Received Frame with SeqNum: %d got corrupted\n", receivedFrame.seqNum);
} else {
printf("---> Received Frame with SeqNum: %d successfully received\n", receivedFrame.seqNum);
}
printf("---> Sending Acknowledgment for SeqNum: %d\n\n", receivedFrame.seqNum); // Send acknowledgment for the received frame
if (rand() % 10 == 0) { // Break the loop randomly with a 10% probability
break;
}
}
}