-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender.c
More file actions
44 lines (32 loc) · 1.58 KB
/
Sender.c
File metadata and controls
44 lines (32 loc) · 1.58 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
// This code snippet implements a basic sender function for a communication system that sends data in frames.
#include <stdio.h>
#include <string.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;
}
// Function to send a frame over a noisy channel
void sendFrame(Frame frame, double errorProb) {
printf("Sending Frame with SeqNum: %d\n", frame.seqNum);
if (isCorrupted(errorProb)) {
printf("Frame with SeqNum: %d got corrupted\n", frame.seqNum);
} else {
printf("Frame with SeqNum: %d successfully received\n", frame.seqNum);
}
}
void sender(char message[], int messageSize, double errorProb) {
int nextSeqNum = 0;
Frame sendingFrame;
for (int i = 0; i < messageSize; i += FRAME_SIZE) {
strncpy(sendingFrame.data, message + i, FRAME_SIZE); // Copy a portion of the message to the frame's data field
sendingFrame.seqNum = nextSeqNum; // Assign the sequence number to the frame
sendFrame(sendingFrame, errorProb); // Call the sendFrame function to send the frame
nextSeqNum = nextSeqNum == 0 ? 1 : 0; // Update the next sequence number
}
}
/**
* The 'sender' function takes a message, breaks it into frames, assigns sequence numbers to each frame,
* and 'sends' these frames using the sendFrame function. The sender alternates between sequence numbers 0 and 1 for each frame.
**/