-
Notifications
You must be signed in to change notification settings - Fork 1
/
abc.cpp
1 lines (1 loc) · 2.34 KB
/
abc.cpp
1
#include<stdio.h>//Defaultforsystemcall#include<sys/socket.h>//Socketcreatingandbinding#include<netinet/in.h>//Neededtousestructsockaddr_in#include<string.h>//Forstringmethod#include<stdlib.h>//Standardlib#include<time.h>//Forcontroltiming#include<iostream>#include<fstream>#include<cerrno>#include<vector>#include<unistd.h>#include<cerrno>#include"Model/messgModel.h"#include"Model/ackModel.h"usingnamespacestd;intudpSocket;structsockaddr_inmyaddr;//thisaddressstructsockaddr_inremaddr;//senderaddresssocklen_taddrlen=sizeof(remaddr);/*lengthofaddresses*/intSERVICE_PORT;char*filename;unsignedintWINDOW_SIZE;unsignedintBUFFER_SIZE;vector<unsignedchar>buffer;unsignedintNFE=0;//nextframeexpectedunsignedintLFA=WINDOW_SIZE-1;//largestframeacceptablevoidwriteToFile(char*filename){ofstreamfout;fout.open(filename,fstream::app);while(!buffer.empty()){fout<<buffer.front();//cout<<"Writing:"<<buffer.front()<<endl;buffer.erase(buffer.begin());}}voidstoreInBuffer(unsignedcharmsg){buffer.push_back(msg);//cout<<"storeinbuffer:"<<msg<<endl;if(buffer.size()==BUFFER_SIZE){cout<<"bufferisfull,emptyitbywritingtofile"<<endl;writeToFile(filename);}elseif(msg=='.'){cout<<"eofdetected,emptytherestofdatainbuffer"<<endl;writeToFile(filename);}}voidrecvMsg(intudpSocket){unsignedcharmsg[9];//boolapproved[WINDOW_SIZE+1];//setAllFalse(approved,WINDOW_SIZE);printf("Waitingonport%d\n",SERVICE_PORT);while(true){//std::this_thread::sleep_for(std::chrono::milliseconds(500));inttest=recvfrom(udpSocket,msg,9,0,(structsockaddr*)&remaddr,&addrlen);messgModeltemp(msg);if(temp.isError())cout<<"Errordetected->messagerejected"<<endl;else{storeInBuffer(temp.getData());if(temp.getSeqNum()==NFE){//asexpectedthenacceptedNFE++;LFA++;}//else,sendACKofexpectedframeACKModeltempAck(NFE,BUFFER_SIZE-buffer.size());unsignedchar*msg=tempAck.setFrameFormat();//sendACKif(sendto(udpSocket,msg,7,0,(structsockaddr*)&remaddr,sizeof(remaddr))<0)cout<<errno<<endl;elsecout<<"sendingACK:"<<tempAck.getNextSeqNum()<<endl;}}}intmain(intargc,char**argv){if(argc<5){perror("<filename><windowsize><buffersize><port>");exit(1);}filename=argv[1];WINDOW_SIZE=atoi(argv[2]);BUFFER_SIZE=atoi(argv[3]);SERVICE_PORT=atoi(argv[4]);intfd;/*createaUDPsocket*/if((fd=socket(AF_INET,SOCK_DGRAM,0))<0){perror("cannotcreatesocket\n");return0;}memset((char*)&myaddr,0,sizeof(myaddr));myaddr.sin_family=AF_INET;myaddr.sin_addr.s_addr=htonl(INADDR_ANY);myaddr.