#include <conio.h> #include <dos.h> #include <stdio.h> #include <stdlib.h> #define TIMEOUT 5 #define MAX_SEQ 1 #define TOT_PACKETS 8 #define inc(k) if(k<MAX_SEQ) k++; else k=0; typedef struct { int data; }packet; typedef struct { int kind; int seq; int ack; packet info; int err; }frame; frame DATA; typedef enum{frame_arrival,err,timeout,no_event} event_type; void from_network_layer(packet *); void to_network_layer(packet *); void to_physical_layer(frame *); void from_physical_layer(frame *); void wait_for_event_sender(event_type *); void wait_for_event_reciever(event_type *); void reciever(); void sender(); int i=1; //Data to be sent by sender char turn; //r , s int DISCONNECT=0; /*__________________________________________________________________________*/ void main() { clrscr(); randomize(); while(!DISCONNECT) { sender(); delay(400); reciever(); } getch(); } /*__________________________________________________________________________*/ void sender() { static int frame_to_send=0; static frame s; packet buffer; event_type event; static int flag=0; if(flag==0) { from_network_layer(&buffer); s.info = buffer; s.seq = frame_to_send; printf("SENDER : Info = %d Seq No = %d ",s.info,s.seq); turn = 'r'; to_physical_layer(&s); flag = 1; } wait_for_event_sender(&event); if(turn=='s') { if(event==frame_arrival) { from_network_layer(&buffer); inc(frame_to_send); s.info = buffer; s.seq = frame_to_send; printf("SENDER : Info = %d Seq No = %d ",s.info,s.seq); turn = 'r'; to_physical_layer(&s); } if(event==timeout) { printf("SENDER : Resending Frame "); turn = 'r'; to_physical_layer(&s); } } } /*__________________________________________________________________________*/ void reciever() { static int frame_expected=0; frame r,s; event_type event; wait_for_event_reciever(&event); if(turn=='r') { if(event==frame_arrival) { from_physical_layer(&r); if(r.seq==frame_expected) { to_network_layer(&r.info); inc(frame_expected); } else printf("RECIEVER : Acknowledgement Resent\n"); turn = 's'; to_physical_layer(&s); } if(event==err) { printf("RECIEVER : Garbled Frame\n"); turn = 's'; //if frame not recieved } //sender shold send it again } } /*__________________________________________________________________________*/ void from_network_layer(packet *buffer) { (*buffer).data = i; i++; } /*___________________________________________________________________________*/ void to_physical_layer(frame *s) { // 0 means error s->err = random(4); //non zero means no error DATA = *s; //probability of error = 1/4 } /*___________________________________________________________________________*/ void to_network_layer(packet *buffer) { printf("RECIEVER :Packet %d recieved , Ack Sent\n",(*buffer).data); if(i>TOT_PACKETS) //if all packets recieved then disconnect { DISCONNECT = 1; printf("\nDISCONNECTED"); } } /*___________________________________________________________________________*/ void from_physical_layer(frame *buffer) { *buffer = DATA; } /*___________________________________________________________________________*/ void wait_for_event_sender(event_type * e) { static int timer=0; if(turn=='s') { timer++; if(timer==TIMEOUT) { *e = timeout; printf("SENDER : Ack not recieved=> TIMEOUT\n"); timer = 0; return; } if(DATA.err==0) *e = err; else { timer = 0; *e = frame_arrival; } } } /*____________________________________________________________________________*/ void wait_for_event_reciever(event_type * e) { if(turn=='r') { if(DATA.err==0) *e = err; else *e = frame_arrival; } } /* OUTPUT SENDER : Info = 1 Seq No = 0 RECIEVER :Packet 1 recieved , Ack Sent SENDER : Ack not recieved=> TIMEOUT SENDER : Resending Frame RECIEVER : Acknowledgement Resent SENDER : Info = 2 Seq No = 1 RECIEVER :Packet 2 recieved , Ack Sent SENDER : Info = 3 Seq No = 0 RECIEVER :Packet 3 recieved , Ack Sent SENDER : Ack not recieved=> TIMEOUT SENDER : Resending Frame RECIEVER : Acknowledgement Resent SENDER : Info = 4 Seq No = 1 RECIEVER :Packet 4 recieved , Ack Sent SENDER : Info = 5 Seq No = 0 RECIEVER :Packet 5 recieved , Ack Sent SENDER : Info = 6 Seq No = 1 RECIEVER :Packet 6 recieved , Ack Sent SENDER : Info = 7 Seq No = 0 RECIEVER :Packet 7 recieved , Ack Sent SENDER : Info = 8 Seq No = 1 RECIEVER : Garbled Frame SENDER : Ack not recieved=> TIMEOUT SENDER : Resending Frame RECIEVER :Packet 8 recieved , Ack Sent DISCONNECTED */
Showing posts with label fraMes. Show all posts
Showing posts with label fraMes. Show all posts
Saturday, March 22, 2014
STOP AND WAIT PROTOCOL PROGRAM IN C++
SELECTIVE REPEAT PROTOCOL PROGRAM IN C++
// SIMULATE SELECTIVE REPEAT PROTOCOL # include <iostream.h> # include <conio.h> # include <stdlib.h> # include <time.h> # include <math.h> # define TOT_FRAMES 500 # define FRAMES_SEND 10 class sel_repeat { private: int fr_send_at_instance; int arr[TOT_FRAMES]; int send[FRAMES_SEND]; int rcvd[FRAMES_SEND]; char rcvd_ack[FRAMES_SEND]; int sw; int rw; // tells expected frame public: void input(); void sender(int); void reciever(int); }; void sel_repeat :: input() { int n; // no of bits for the frame int m; // no of frames from n bits cout << "Enter the no of bits for the sequence number "; cin >> n; m = pow (2 , n); int t = 0; fr_send_at_instance = (m / 2); for (int i = 0 ; i < TOT_FRAMES ; i++) { arr[i] = t; t = (t + 1) % m; } for (i = 0 ; i < fr_send_at_instance ; i++) { send[i] = arr[i]; rcvd[i] = arr[i]; rcvd_ack[i] = 'n'; } rw = sw = fr_send_at_instance; sender(m); } void sel_repeat :: sender(int m) { for (int i = 0 ; i < fr_send_at_instance ; i++) { if ( rcvd_ack[i] == 'n' ) cout << " SENDER : Frame " << send[i] << " is sent\n"; } reciever (m); } void sel_repeat :: reciever(int m) { time_t t; int f; int f1; int a1; char ch; srand((unsigned) time(&t)); for (int i = 0 ; i < fr_send_at_instance ; i++) { if (rcvd_ack[i] == 'n') { f = rand() % 10; // if = 5 frame is discarded for some reason // else frame is correctly recieved if (f != 5) { for (int j = 0 ; j < fr_send_at_instance ; j++) if (rcvd[j] == send[i]) { cout << "RECIEVER : Frame " << rcvd[j] << " recieved correctly\n"; rcvd[j] = arr[rw]; rw = (rw + 1) % m; break; } if (j == fr_send_at_instance) cout << "RECIEVER : Duplicate frame " << send[i] << " discarded\n"; a1 = rand() % 5; // if a1 == 3 then ack is lost // else recieved if (a1 == 3) { cout << "(Acknowledgement " << send[i] << " lost)\n"; cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n"; rcvd_ack[i] = 'n'; } else { cout << "(Acknowledgement " << send[i] << " recieved)\n"; rcvd_ack[i] = 'p'; } } else { int ld = rand() % 2; // if = 0 then frame damaged // else frame lost if (ld == 0) { cout << "RECIEVER : Frame " << send[i] << " is damaged\n"; cout << "RECIEVER : Negative acknowledgement " << send[i] << " sent\n"; } else { cout << "RECIEVER : Frame " << send[i] << " is lost\n"; cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n"; } rcvd_ack[i] = 'n'; } } } for ( int j = 0 ; j < fr_send_at_instance ; j++) { if (rcvd_ack[j] == 'n') break; } i = 0 ; for (int k = j ; k < fr_send_at_instance ; k++) { send[i] = send[k]; if (rcvd_ack[k] == 'n') rcvd_ack[i] = 'n'; else rcvd_ack[i] = 'p'; i++; } if ( i != fr_send_at_instance ) { for ( int k = i ; k < fr_send_at_instance ; k++) { send[k] = arr[sw]; sw = (sw + 1) % m; rcvd_ack[k] = 'n'; } } cout << "Want to continue..."; cin >> ch; cout << "\n"; if (ch == 'y') sender(m); else exit(0); } void main() { clrscr(); sel_repeat sr; sr.input(); getch(); } /* OUTPUT Enter the no of bits for the sequence number 6 SENDER : Frame 24 is sent SENDER : Frame 32 is sent SENDER : Frame 32 is sent SENDER : Frame 27 is sent SENDER : Frame 28 is sent SENDER : Frame 29 is sent SENDER : Frame 30 is sent SENDER : Frame 31 is sent RECIEVER : Frame 24 is lost (SENDER TIMEOUTS --> RESEND THE FRAME) RECIEVER : Frame 32 recieved correctly (Acknowledgement 32 recieved) RECIEVER : Frame 33 recieved correctly (Acknowledgement 34 recieved) RECIEVER : Frame 27 recieved correctly (Acknowledgement 34 recieved) RECIEVER : Frame 28 is damaged RECIEVER : Negative acknowledgement 28 sent RECIEVER : Frame 29 recieved correctly (Acknowledgement 35 recieved) RECIEVER : Frame 30 recieved correctly (Acknowledgement 36 lost) (SENDER TIMEOUTS --> RESEND THE FRAME) RECIEVER : Frame 31 recieved correctly (Acknowledgement 37 recieved) Want to continue...n */
Labels:
c plus plus,
computer networks,
cpp program,
csa,
fraMes,
networks,
selective repeat protocol
GO BACK N PROTOCOL PROGRAM IN C++
# include <iostream.h> # include <conio.h> # include <stdlib.h> # include <time.h> # include <math.h> # define TOT_FRAMES 500 # define FRAMES_SEND 10 class gobkn { private: int fr_send_at_instance; int arr[TOT_FRAMES]; int arr1[FRAMES_SEND]; int sw; int rw; // tells expected frame public: gobkn(); void input(); void sender(int); void reciever(int); }; gobkn :: gobkn() { sw = 0; rw = 0; } void gobkn :: input() { int n; // no of bits for the frame int m; // no of frames from n bits cout << "Enter the no of bits for the sequence no "; cin >> n; m = pow (2 , n); int t = 0; fr_send_at_instance = (m / 2); for (int i = 0 ; i < TOT_FRAMES ; i++) { arr[i] = t; t = (t + 1) % m; } sender(m); } void gobkn :: sender(int m) { int j = 0; for (int i = sw ; i < sw + fr_send_at_instance ; i++) { arr1[j] = arr[i]; j++; } for (i = 0 ; i < j ; i++) cout << " SENDER : Frame " << arr1[i] << " is sent\n"; reciever (m); } void gobkn :: reciever(int m) { time_t t; int f; int f1; int a1; char ch; srand((unsigned) time(&t)); f = rand() % 10; // if = 5 frame is discarded for some reason // else they are correctly recieved if (f != 5) { for (int i = 0 ; i < fr_send_at_instance ; i++) { if (rw == arr1[i]) { cout << "RECIEVER : Frame " << arr1[i] << " recieved correctly\n"; rw = (rw + 1) % m; } else cout << "RECIEVER : Duplicate frame " << arr1[i] << " discarded\n"; } a1 = rand() % 15; // if a1 belongs to 0 to 3 then // all ack after this (incl this one) lost // else // all recieved if (a1 >= 0 && a1 <= 3) { cout << "(Acknowledgement " << arr1[a1] << " & all after this lost)\n"; sw = arr1[a1]; } else sw = (sw + fr_send_at_instance) % m; } else { f1 = rand() % fr_send_at_instance; // f1 gives index of the frame being lost for (int i = 0 ; i < f1 ; i++) { if (rw == arr1[i]) { cout << " RECIEVER : Frame " << arr1[i] << " recieved correctly\n"; rw = (rw + 1) % m; } else cout << " RECIEVER : Duplicate frame " << arr1[i] << " discarded\n"; } int ld = rand() % 2; // ld == 0 frame damaged // else frame lost if (ld == 0) cout << " RECIEVER : Frame " << arr1[f1] << " damaged\n"; else cout << " (Frame " << arr1[f1] << " lost)\n"; for (i = f1 + 1 ; i < fr_send_at_instance ; i++) cout << " RECIEVER : Frame " << arr1[i] << " discarded\n"; cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n"; sw = arr1[f1]; } cout << "Want to continue..."; cin >> ch; if (ch == 'y') sender(m); else exit(0); } void main() { clrscr(); gobkn gb; gb.input(); getch(); } /* OUTPUT Enter the no of bits for the sequence no 4 SENDER : Frame 0 is sent SENDER : Frame 1 is sent SENDER : Frame 2 is sent SENDER : Frame 3 is sent SENDER : Frame 4 is sent SENDER : Frame 5 is sent SENDER : Frame 6 is sent SENDER : Frame 7 is sent RECIEVER : Frame 0 recieved correctly RECIEVER : Frame 1 recieved correctly RECIEVER : Frame 2 recieved correctly RECIEVER : Frame 3 recieved correctly RECIEVER : Frame 4 recieved correctly RECIEVER : Frame 5 recieved correctly RECIEVER : Frame 6 recieved correctly RECIEVER : Frame 7 recieved correctly Want to continue...n */
Labels:
c plus plus,
computer networks,
cpp program,
fraMes,
go back n protocol,
networks
Subscribe to:
Posts (Atom)