0% found this document useful (0 votes)
2 views2 pages

Stop

Uploaded by

sreeloveyou20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Stop

Uploaded by

sreeloveyou20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<stdlib.h>
#include<stdbool.h>
#include<unistd.h>
#define Packet_size 1024
#define TIMEOUT 5
typedef struct{
int seq_no;
bool acked;
}Frame;
void sendPacket(Frame frame){
sleep(1);
printf("Sending frame with sequence number :%d\n",frame.seq_no);
}
bool receiveACK(){
sleep(1);
return rand()%10 != 0;
}
void sendNACK(Frame frame){
sleep(1);
printf("Sending NACK for sequence number : %d\n",frame.seq_no);
}
void sendFrames(int max_seq_no){
int next_seq_no = 0;
while(next_seq_no<max_seq_no){
Frame frame={next_seq_no,false};
sendPacket(frame);
bool ackReceived = false;
int timeout= TIMEOUT;
while(!ackReceived && timeout>0){
if(receiveACK()){
ackReceived=true;
frame.acked=true;
printf("frame with sequence number %d acknowledged.\n",frame.seq_no);
}
else{
sendNACK(frame);
printf("Timeout occurred.resending frame with sequence number %d\n",frame.seq_no);
timeout--;
}
}
if(!ackReceived){
printf("Failed to receive ACK after multiple attempts. Exiting.\n");
return;
}
next_seq_no++;
}
}
int main(){
int max_seq_no;
printf("Enter Maximum sequence number:");
scanf("%d",&max_seq_no);
srand(time(NULL));
sendFrames(max_seq_no);
return 0;
}

You might also like