EX NO 7b
SIMUALTION OF STOP AND WAIT PROTOCOL
Aim
To write a program in C to simulate the Stop and Wait Protocol
Algorithm:
[Link] the program
2. Get the frame size from the user
[Link] create the frame based on the user request
[Link] frames to server from the client side.
[Link] your frames reach the server, it will send ACK signal to client otherwise it will send NACK
signal to client
6. Stop the program.
Program:
Sender
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 100
typedef struct
{
unsigned char data[MAXSIZE];
}packet;
typedef enum{data,ack}frame_kind;
typedef struct
{
frame_kind kind;
int sq_no;
int ack;
packet info;
}frame;
typedef enum{frame_arrival}event_type;
typedef enum{true_false}boolean;
void frame_network_layer(packet *p)
{
printf("\n from network arrival");
}
void to_physical_layer(frame *f)
{
printf("\n to physical layer");
}
void wait_for_event(event_type *e)
{
printf("\n waiting for event n");
}
void sender(void)
{
frame s;
packet buffer;
event_type event;
printf("\n ***SENDER***");
frame_network_layer(&buffer);
[Link]=buffer;
to_physical_layer(&s);
wait_for_event(&event);
}
void from_physical_layer(frame *f)
{
printf("from physical layer");
}
void to_network_layer(packet *p)
{
printf("\n to network layer");
}
void receiver(void)
{
frame r,s;
event_type event;
printf("\n ***RECEIVER***");
wait_for_event(&event);
from_physical_layer(&r);
to_network_layer(&[Link]);
to_physical_layer(&s);
int main()
{
int i, n, ch;
char input[20];
FILE *in;
rand();
printf(\n\t\t Stop and Wait Protocol \n);
printf(\n 1. Send \n2. Check ACK\[Link]\n);
while(1)
{
printf(Enter your choice.);
scanf(%d, &ch);
switch(ch)
{
case 1:
in = fopen([Link], w);
printf(Enter the Data: );
scanf(%s, input);
n = strlen(input);
for(i=0; i<n+1; i++)
fprintf(in, %s, input);
fclose(in);
printf(->Data Sent\n);
break;
getch();
case2:
sender();
receiver();
break;
case 3:exit();
}
Receiver
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i, n,;
char output[20];
FILE *out;
clrscr();
out=fopen(data_parity.txt, r);
fscanf(out, %s, output);
n = strlen(output);
for(i=0; i<n; i++)
if(output[i]==1)
one++;
if(one%2==0)
{
printf(Received Data has Even Parity\n);
printf(Data Accepted\n);
}
else
{
printf(Received Data has Odd Parity\n);
printf(Data Rejected\n);
}
printf(Received Data: );
for(i=0;i<n-1; i++)
printf(%c, output[i];
fclose(out);
getch();
}
OUTPUT:
***SENDER***
from network arrival
to physical layer
waiting for an event n
***RECEIVER***
waiting for an event n from physical layer
to network layer
to physical layerPress any key to continue...
Result:
Thus the stop and wait protocol was implemented successfully.