0% found this document useful (0 votes)
222 views10 pages

Go Back N ARQ

The document contains C code for implementing a sliding window protocol for reliable data transmission between a sender and receiver. The sender code defines a frame structure, inserts data and sequence numbers into frames, and sends frames in a windowed manner. It can optionally introduce errors or drop frames. The receiver code receives frames, checks for errors, acknowledges correctly received frames, and discards out-of-order frames. The code compiles and runs a sample session between the sender and receiver processes.

Uploaded by

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

Go Back N ARQ

The document contains C code for implementing a sliding window protocol for reliable data transmission between a sender and receiver. The sender code defines a frame structure, inserts data and sequence numbers into frames, and sends frames in a windowed manner. It can optionally introduce errors or drop frames. The receiver code receives frames, checks for errors, acknowledges correctly received frames, and discards out-of-order frames. The code compiles and runs a sample session between the sender and receiver processes.

Uploaded by

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

SOURCE CODE:

SENDER:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
#include<unistd.h>
typedef struct frame_struct
{
int seqnum;
char data[10];
char header[10];
char trailer[10];
char sequence[2];
}frame;
frame insert (char var[10],int seq)
{
frame f;
int i,c=0;
for(i=0;i<7;i++)
{
if(var[i]=='1')
c++;
}
if((c%2)!=0)
var[7]='1';
else
var[7]='0';
var[8]='\0';
strcpy(f.data,var);
f.seqnum=seq;
switch(f.seqnum)
{
case 0: strcpy(f.sequence,"00");break;
case 1: strcpy(f.sequence,"01");break;
case 2: strcpy(f.sequence,"10");break;
case 3: strcpy(f.sequence,"11");break;
}
strcpy(f.header,"01111110");
strcpy(f.trailer,"01111110");
return f;
}
void print(frame head)
{
printf("\n%s-%s-%s-%s",head.header,head.sequence,head.data,head.trailer);
}
int main()
{
int laf=-1,lfs=-1,sws=3,ch1,ch2,pos,ack,k,fc,ind;
int fd,sd,i,count,j=0,snum=0,n,r,l;
char buf[50],fr[200][10],c;
frame f[20],temp;
struct sockaddr_in sa;
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
perror("\n Socket error");
bzero(&sa,sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=INADDR_ANY;
sa.sin_port=htons(61125);
l=sizeof(sa);
fd=open("frame.txt",0,"O_RDWR");
if(fd==-1)
printf("\nERROR");
count=0;
i=0;
while((r=read(fd,&c,sizeof(char)))>0)
{
if(count<6)
{
fr[i][count++]=c;
}
else
{
fr[i][count++]=c;
fr[i][count]='\0';
count=0;
i++;
}
}
n=i;
for(j=0;j<n;j++)
{
f[j]=insert(fr[j],snum);
snum++;
if(snum>3)
snum=0;
}
for(j=0;j<n;j++)
print(f[j]);
j=0;
while(laf<n-1)
{
if(((lfs-laf)<sws)&&(j<n))
{
{
printf("\nWINDOW\n");
for(ind=laf+1;ind<laf+4;ind++)
if(ind<n)
printf("\t%d",f[ind].seqnum);
}
print(f[j]);
printf("\nDo u want to introduce error ");
scanf("%d",&ch2);
if(ch2==1)
{
printf("\nEnter the position ");
scanf("%d",&pos);
temp=f[j];
if(f[j].data[pos]=='0')
f[j].data[pos]='1';
else
f[j].data[pos]='0';
printf("Error frame:");
print(f[j]);
}
printf("\nDo the frame get lost ");
scanf("%d",&ch1);
if(ch1==1)
{
if(ch2==1)
{
if(f[j].data[pos]=='0')
f[j].data[pos]='1';
else
f[j].data[pos]='0';
}
lfs++;
j++;
continue;
}
else
{
sendto(sd,&f[j],sizeof(f[j]),0,(struct sockaddr*)&sa,sizeof(sa));
if(ch2==1)
{
if(f[j].data[pos]=='0')
f[j].data[pos]='1';
else
f[j].data[pos]='0';
}
recvfrom(sd,&buf,sizeof(buf),0,(struct sockaddr *)&sa,&l);
if(strcmp(buf," ")!=0)
{
int array=0;
ack=(int)buf[3];
array*=10;
array+=(int)buf[3];
array-=48;
ack=array;
ack--;
if(ack<0)
ack=sws;
k=laf+1;
fc=0;
while(fc<sws)
{
if(f[k].seqnum==ack)
break;
fc++;
k++;
}
laf=k;
}
lfs++;
j++;
}
}
else
{
j=laf+1;
lfs=laf;
printf("\nend of window %d",j);
}
}
printf("\nAll frames sent and acknowledged");
}
RECEIVER:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
#include<unistd.h>
typedef struct frame_struct
{
int seqnum;
char data[10];
char header[10];
char trailer[10];
char sequence[2];
}frame;
void print(frame head)
{
printf("\n%s %s %s %s",head.header,head.sequence,head.data,head.trailer);
}
int main()
{
int r=0,rcv=0;
int fd,sd,i,count,j=0,snum=0,n,l,ch;
char buf[50],fr[200][10],c;
frame f[20],temp;
struct sockaddr_in sa;
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
perror("\n Socket error");
bzero(&sa,sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=inet_addr("127.0.0.1");
sa.sin_port=htons(61125);
if(bind(sd,(struct sockaddr *)&sa,sizeof(sa)))
perror("bind error");
l=sizeof(sa);
while(1)
{
printf("\nExpected frame %d",r);
recvfrom(sd,&temp,sizeof(temp),0,(struct sockaddr *)&sa,&l);
if(r==temp.seqnum)
{
c=0;
for(i=0;i<8;i++)
{
if(temp.data[i]=='1')
c++;
}
if((c%2)==0)
{
f[rcv++]=temp;
printf("\n%s-%s-%s-%s",temp.header,temp.sequence,temp.data,temp.trailer);
r=(r+1)%4;
printf("\nDo u want to send acknowledgement?");
scanf("%d",&ch);
if(ch==1)
{
strcpy(buf,"ack");
buf[3]=48+r;
buf[4]='\0';
}
else
strcpy(buf," ");
sendto(sd,buf,sizeof(buf),0,(struct sockaddr*)&sa,sizeof(sa));
}
else
{
strcpy(buf," ");
sendto(sd,buf,sizeof(buf),0,(struct sockaddr*)&sa,sizeof(sa));
printf("\nERROR");
printf("\n");
}
}
else
{
int fl=0,i;
for(i=0;i<rcv;i++)
{
if(strcmp(f[i].data,temp.data)==0)
{
fl=1;
strcpy(buf,"ack");
buf[3]=48+(f[i].seqnum+1);
printf("\nFrame discarded ack resent");
}
}
if(fl!=1)
{
printf("\nBut received :");
printf("\n%s-%s-%s-%s",temp.header,temp.sequence,temp.data,temp.trailer);
printf("\nReceived out of order hence discarded");
strcpy(buf," ");
}
sendto(sd,buf,sizeof(buf),0,(struct sockaddr *)&sa,sizeof(sa));
}
}
}
SAMPLE I/O:
SENDER:
RECEIVER:

You might also like