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);
}

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.