postheadericon Protocol 3 A Simplex Protocol for a Noisy Channel

header.h


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
//define Frame structure
struct frame
{
int frame_type;//{1-frame, 0-ack}
int seqno;
int ackno;
char data[50];
};
//services used @ sender side
void from_network_layer(char buffer[])
{
printf("\nEnter Data : ");
scanf("%s",buffer);
}
void to_physical_layer(struct frame *f)
{
int pid1;
system(">pipe1");
pid1=open("pipe1",O_WRONLY);
write(pid1,f,sizeof(struct frame));
close(pid1);
if(f->frame_type==1)
{
printf("\n\n\tFrame [%d]-[%s] sucessfully send.\n",f->seqno,f->data);
}
else
{
printf("\n\n\tAcknowledment [%d]-[%s] sucessfully send.\n",f->ackno,f->data);
}
}
int get_ack()
{
int pid2,ackno=0;
pid2=open("pipe2",O_RDONLY);
if(pid2>0)
{
read(pid2,&ackno,sizeof(ackno));
unlink("pipe2");
}
close(pid2);
return ackno;
}
int start_timer(int seqno)
{
printf("\nTimer Starts");
int i=1,flag=1;
for(i=1;i<=10;i++)
{
sleep(1);
if(seqno==get_ack())
{
flag=0;
break;
}
printf("\ntime : %d ",i);
}
return flag;
}
//services used @ reciving side
void from_physical_layer(struct frame *f)
{
int pid1;
pid1=open("pipe1",O_RDONLY);
f->seqno=0;
if(pid1>0)
{
read(pid1,f,sizeof(struct frame));
unlink("pipe1");
}
close(pid1);
}

void to_network_layer(struct frame *f)
{

printf("\n\nframe No [ %d ]-[%s]received successfully\n",f->seqno,f->data);

}
void send_ack(int seqno)
{
int pid2;
system(">pipe2");
pid2=open("pipe2",O_WRONLY);
if(pid2>0)
{
write(pid2,&seqno,sizeof(seqno));
}
close(pid2);
}



sender


#include "header.h"
void main()
{
int no,i;
char buffer[50];
struct frame f;
printf("Enter How Many Frame you want to send : ");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
from_network_layer(buffer);
strcpy(f.data,buffer);
f.seqno=i;

f.frame_type=1;
do
{
to_physical_layer(&f);
}while(start_timer(f.seqno));
}
//send ack for terminate the connection
strcpy(f.data,"terminate");
f.seqno=-1;
f.ackno=1;
f.frame_type=0;
to_physical_layer(&f);
}

receiver


#include "header.h"
void main()
{
int no,i,frame_expected=1;
char buffer[50];
struct frame f;

while(1)
{
sleep(1);
from_physical_layer(&f);

if(f.seqno!=frame_expected)
{
if(f.seqno==-1)
{
printf("\n\nTransmision Compilted...\n Total [ %d ] frame received\n\n",frame_expected-1);
break;

}
else
{
printf("\nwaiting for frame [ %d ]",frame_expected);
send_ack(f.seqno);
continue;
}

}
else
{

frame_expected++;
strcpy(buffer,f.data);
to_network_layer(&f);
send_ack(f.seqno);
}
}
}

0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.