postheadericon Protocol-2 stop-and-wait protocol

header file


/*
Protocol-2 (stop-and-wait) ,
one-directional flow of data from sender to receiver,
error free channel, The receiver has only a finite buffer capacity and a finite
processing speed, protocol must prevent the sender from flooding
*/

#include<stdio.h>
#include<fcntl.h>
#include<string.h>

//define Frame structure
typedef struct
{
int seqno;
int ackno;
char data[100];
}frame;


//services used @ sender side
void from_network_layer(char buffer[],int sno)
{
printf("Enter Frame [%d] : ",sno);
scanf("%s",buffer);
}

void to_physical_layer(frame *f)
{
int pid1;
system(">pipe1");
pid1=open("pipe1",O_WRONLY);
write(pid1,f,sizeof(frame));
close(pid1);
}

void wait_for_event(int seq_no)
{
int pid2,no;
while(1)
{
pid2=open("pipe2",O_RDONLY);
read(pid2,&no,sizeof(no));
 
if(no==seq_no)
break;

}
close(pid2);
unlink("pipe2");
}

//services used @ reciving side
void from_physical_layer(frame *f)
{
int pid1;
pid1=open("pipe1",O_RDONLY);
read(pid1,f,sizeof(frame));
close(pid1);
unlink("pipe1");
}

void to_network_layer(char buffer[])
{
printf("\nFrame  : %s",buffer);
}

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


Sender




#include "header.h"

void main()
{
int i,no,pids;
char buffer[100];
frame f;

printf("How many frame you want to send? : ");
scanf("%d",&no);


for(i=0;i<no;i++)
{
from_network_layer(buffer,i);/*  get some data from network layer to send next layer */
strcpy(f.data,buffer);/*  copy received data into frame's pay load for transmission */
f.seqno=i;
to_physical_layer(&f); /*  send our frame on physical layer */
wait_for_event(i); /* block sender until sender received acknowledment from receciver*/
}
}


Receiver



#include "header.h"

void main()
{
int no,i;
char buffer[100];
frame f;

from_physical_layer(&f); /*  get some data from physical layer to send network layer */

strcpy(buffer,f.data); /*  copy received data into buffer */

to_network_layer(buffer); /*  send our received data to network layer */

send_ack(f.seqno); /*send aknowlegment to the sender that we got Frame[f.seqno] */

}


1 comments:

Unknown said...

nice

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.