- #include<iostream>
- #include<cmath>
- #include<conio.h>
- #include<stdlib.h>
- #include<ctime>
- using namespace std;
- class Q4{
- public:
- int a,m,s,*arr,ws;
- Q4();
- void correctframe(int);
- void acknowledgementlost(int);
- void framelost(int);
- void delayinframe(int);
- void window(int);
- void resend(int);
- };
- Q4::Q4(){
- cout<<"Enter the number of frames: ";
- cin>>a;
- cout<<"Enter the window size(value of m): ";
- cin>>m;
- s=pow(2,m-1);
- cout<<"Window size:"<<s<<endl;
- ws=pow(2,m);
- arr=new int[s];
- cout<<"Sequence of sending frame :"<<ws<<endl;
- cout<<"Sequencial numbers of rame to be used :";
- for(int i=0;i<ws;i++)
- {
- cout<<i<<" ";
- }cout<<endl;
- }
- void Q4::correctframe(int i)
- {
- cout<<"\t\t\tRecieving Frames from Sender"<<endl;
- cout<<"\t\t\t\tSending acknowledgement to sender"<<endl;
- getch();
- cout<<"Acknowledgement recieved for frame f"<<(i-1)%ws<<"\n";
- getch();
- }
- void Q4::acknowledgementlost(int i)
- {
- cout<<"\t\t\tRecieving Frames from Sender"<<endl;
- cout<<"\t\t\t\tSending acknowledgement to sender"<<endl;
- getch();
- cout<<"\t\t Acknowledgement lost by the reciever for frame f"<<(i-1)%ws<<"\n";
- getch();
- resend(i);
- getch();
- cout<<"\t\t\t\tSending acknowledgement to sender"<<endl;
- cout<<"Acknowledgement recieved for frame f"<<(i-1)%ws<<"\n";
- getch();
- cout<<"Sliding Window....Sending next frame"<<endl;
- getch();
- }
- void Q4::framelost(int i)
- {
- cout<<"\t\t\tRecieving Frames from Sender"<<endl;
- cout<<"\t\t Frame f"<<(i-1)%ws<<" is lost\n";
- getch();
- resend(i);
- getch();
- cout<<"\t\t\t\tSending acknowledgement to sender"<<endl;
- getch();
- cout<<"Acknowledgement recieved for frame f"<<(i-1)%ws<<"\n";
- getch();
- cout<<"Sliding Window....Sending next frame"<<endl;
- getch();
- }
- void Q4::delayinframe(int i)
- {
- cout<<"\t\t\tRecieving Frames from Sender"<<endl;
- cout<<"\t\t There is a delay in sending the frame f"<<(i-1)%ws<<" from the sender\n";
- getch();
- resend(i);
- getch();
- cout<<"\t\t\t\tSending acknowledgement to sender"<<endl;
- getch();
- cout<<"Acknowledgement recieved for frame f"<<(i-1)%ws<<"\n";
- getch();
- cout<<"Sliding Window....Sending next frame"<<endl;
- getch();
- }
- void Q4::resend(int i){
- cout<<"\nFrames in the current window are: ";
- for(int j=0;j<s;j++){
- if((j+i)<=a){
- arr[j]=j+i;
- cout<<arr[j]<<" ";
- }
- }
- cout<<endl;
- getch();
- cout<<"\nSequencial number Of Frames in window: ";
- for(int j=0;j<s;j++){
- if((j+i)<=a){
- arr[j]=j+i;
- cout<<(i+j-1)%ws<<" ";
- }
- }
- cout<<endl;
- getch();
- /* for(int j=0;j<s;j++){
- arr[j]=j+i;
- if((j+i)<=a){
- cout<<"Frame f"<<(j+i-1)%ws<<" \n";
- }
- }*/
- int j=0;
- if(j+i<=a)
- {
- cout<<"Resending frame f"<<(i-1)%ws<<endl;
- getch();
- j++;
- }
- }
- void Q4::window(int i){
- cout<<endl;
- for(int j=0;j<s;j++){
- if((j+i)<=a){
- arr[j]=j+i;
- if(i==1){
- cout<<"Sending frame f"<<(j+i-1)%ws<<"\n";
- }
- }
- }
- if(i!=1&&i<=a-s+1){
- cout<<"Sending frame f"<<arr[s-1]<<"\n";
- getch();
- }
- }
- int main(){
- Q4 obj;
- int r;
- srand(time(0));
- cout<<"SENDER'S SIDE\n RECIEVER'S SIDE"<<endl;
- for(int i=1;i<=obj.a;i++){
- r=rand()%4;
- obj.window(i);
- switch(r){
- case 0:{
- obj.correctframe(i);
- break;
- }
- case 1:{
- obj.acknowledgementlost(i);
- break;
- }
- case 2:{
- obj.framelost(i);
- break;
- }
- case 3:{
- obj.delayinframe(i);
- break;
- }
- default:
- cout<<"Invalid choice\n";
- }
- }
- cout<<endl<<endl<<"All frames are sent successfully";
- getch();
- return 0;
- }
