File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Stop-and-Wait Protocol (Noisy Channel) Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ // This code snippet implements a basic sender function for a communication system that sends data in frames.
2+
3+
4+
5+ #include <stdio.h>
6+ #include <string.h>
7+ #include <stdlib.h>
8+ #include "Frame.h"
9+
10+
11+ // Determine if the frame is corrupted based on the error probability
12+ bool isCorrupted (double errorProb ) {
13+ return ((double ) rand () / RAND_MAX ) < errorProb ;
14+ }
15+
16+ // Function to send a frame over a noisy channel
17+ void sendFrame (Frame frame , double errorProb ) {
18+ printf ("Sending Frame with SeqNum: %d\n" , frame .seqNum );
19+
20+ if (isCorrupted (errorProb )) {
21+ printf ("Frame with SeqNum: %d got corrupted\n" , frame .seqNum );
22+ } else {
23+ printf ("Frame with SeqNum: %d successfully received\n" , frame .seqNum );
24+ }
25+ }
26+
27+ void sender (char message [], int messageSize , double errorProb ) {
28+ int nextSeqNum = 0 ;
29+ Frame sendingFrame ;
30+ for (int i = 0 ; i < messageSize ; i += FRAME_SIZE ) {
31+ strncpy (sendingFrame .data , message + i , FRAME_SIZE ); // Copy a portion of the message to the frame's data field
32+ sendingFrame .seqNum = nextSeqNum ; // Assign the sequence number to the frame
33+ sendFrame (sendingFrame , errorProb ); // Call the sendFrame function to send the frame
34+ nextSeqNum = nextSeqNum == 0 ? 1 : 0 ; // Update the next sequence number
35+ }
36+ }
37+
38+
39+
40+
41+ /**
42+ * The 'sender' function takes a message, breaks it into frames, assigns sequence numbers to each frame,
43+ * and 'sends' these frames using the sendFrame function. The sender alternates between sequence numbers 0 and 1 for each frame.
44+ **/
You can’t perform that action at this time.
0 commit comments