C Program For Stop And Wait Protocol

Posted on by  admin
Hi Experts
i need some modification in already made that works fine with no error:
I have the following code for sender.c
#include 'assign1.h'
#include 'sender.h'
extern int sTOr[2];
extern int rTOs[2];
FILE *fp;
static FRAME sendFrame;
void sender(char filename[]) {
short blockNum = 0;
char reply[5];
sigignore(SIGINT);
sendFrame.SOH = 00000001;
sendFrame.blockNum=0;
if ((fp = fopen(filename, 'r')) NULL) {
perror('receiver(): fopen: ');
exit(errno);
}
while ((readFile(sendFrame.msg)) > 0) {
++sendFrame.blockNum;
sendFrame.blockNumComp = ~sendFrame.blockNum;
sendFrame.checksum = makeChecksum(sendFrame.msg);
printf('nSENDER: blockNum = %d blockNumComp = %d checksum = %dn', sendFrame.blockNum,
sendFrame.blockNumComp, sendFrame.checksum);
write(sTOr[1], &sendFrame, sizeof(FRAME));
while(1) {
read(rTOs[0], reply, sizeof('ACK'));
printf('%s was receivedn', reply);
if (!strcmp(reply, 'NAK')) {
printf('SENDER2: blockNum = %d blockNumComp = %d checksum = %dn', sendFrame.blockNum,
sendFrame.blockNumComp, sendFrame.checksum);
write(sTOr[1], &sendFrame, sizeof(FRAME));
}
else if (!strcmp(reply, 'ACK'))
break;
else
exit(12);
}
}
}
int readFile(char *str) {
int x;
for(x = 0; x < 128; x++)
str[x] = '0';
return fread(str, sizeof(char), 128, fp);
}
and the following code for receiver.c
#include 'assign1.h'
#include 'receiver.h'
FRAME rFrame;
void receiver() {
sigset(SIGINT, makeError);
while (1) {
if (read(STDIN_FILENO, &rFrame, sizeof(FRAME)) > 0) {
sleep(1);
fprintf(stderr, 'RECEIVER: blockNum = %d blockNumComp = %d checksum = %dn', rFrame.blockNum,
rFrame.blockNumComp, rFrame.checksum);
if (!checkSOH(rFrame.SOH)
!checkBlockNum(rFrame.blockNum, rFrame.blockNumComp)
!checkChecksum(rFrame.msg, rFrame.checksum))
write(STDOUT_FILENO, 'NAK', sizeof('NAK'));
else
write(STDOUT_FILENO, 'ACK', sizeof('ACK'));
}
}
}
int checkSOH(char SOH){
return (SOH 00000001);
}
int checkBlockNum(const short blockNum, const short blockNumComp) {
short newComp = ~blockNum;
if (newComp blockNumComp)
return 1; // No error
return 0; // Error
}
int checkChecksum(char *str, const short checksum) {
if (makeChecksum(str) checksum)
return 1; // No error
return 0; // Error
}
void makeError() {
int n = rand()%3;
switch (n) {
case 0:
rFrame.blockNum = 0;
fputs('nCHANGED BLOCK NUMBERn', stderr);
break;
case 1:
rFrame.checksum = 0;
fputs('nCHANGED CHECKSUMn', stderr);
break;
case 2:
rFrame.blockNumComp = 10;
fputs('nCHANGED BLOCK NUMBER COMPLIEMENTn', stderr);
break;
}
}
The program simulates different errors that the protocol checks for.I need to modify the program so that it implements 'time out'.
Theory:
Sender: sends a frame and calls a SIGALRM function and sets the timer for say, 2 seconds. If the sender gets a response within two seconds from the receiver (either 'ACK' or 'NAK'), turn off the alarm and transmit the next frame. If no response is received, time out function kicks in and retransmits the frame.
Receiver: When it sees an error in transmission, do not send back anything to the sender and this causes the to time out and retransmit.
Im new to C language please help me solve my problem
regards
shaukat

Comments are closed.