void printServerData(void* f) { string unit; //int* freq = (int*) f; int freq = 1000; double r; long now; int tcp, udp; //cout << "serData.ttlByteRecv: " << serData.ttlByteRecv << endl; cout << " printServerData ready" << endl; do{ //mutex mutx.lock(); r = serData.ttlByteRecv + 0.0; tcp = serData.tcpCount; udp = serData.udpCount; mutx.unlock(); //mutex now = x_serTimer.ElapsedX(); if(r>0) r = (r*1000.0)/((double)now - (double)time_serBegin); //byte per ms unit = "Bps"; if(r >= 1024) { r /= 1024; //KBps unit = "KBps"; } if (r >= 1024) { r /= 1024; //MBps; unit = "MBps"; } //mutex printf("Rate [%.4g%s] ", r, unit.c_str()); printf("TCP Clients [%d] ", tcp); printf("UDP Clients [%d]", udp); fflush(stdout); //mutex //cout << "got now" << endl; goToSleep(freq); #ifdef WIN32 cout << '\r' ; #else printf("\033[2K\r"); #endif }while(freq>=0); if(freq<0) { cout << endl; exit(0); } return; }
int UDPChild(void* Ptr){ struct Thread *Pt = (struct Thread*)Ptr; //Initialize parameters SOCKET UDPSend; socklen_t slen; int ArrIndex = Pt->Arr; int PktSize = Pt->PacketSize; int ContrlRate = Pt->ControlRate; int ContrlPkt = Pt->ControlNumber; int Sqn = 0; //Sequential Number long Pkts = 0; double Time = 0; double ThreadRate = 0; double StdElapsed = 0; double SlpTime = 0; mtx_lock(&MutexS); Pt->UDPNum = Pt->UDPNum + 1; //TCP client number mtx_unlock(&MutexS); struct sockaddr_in UDPAddr; UDPAddr = Pt->ClientAddr; slen = sizeof(UDPAddr); char *SendBuf; SendBuf = (char *)malloc(sizeof(char)*PktSize); //Initialize winsock #ifdef WIN32 WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0){ printf("Failed to Initialize, Error code : %d\n", WSAGetLastError()); exit(EXIT_FAILURE); } #endif if ((UDPSend = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET){ printf("Could not create socket : d%\n", WSAGetLastError()); } //Start a timer ES_FlashTimer UDPst = ES_FlashTimer(); UDPst.Start(); //Send data to UDP client with sequential number while (1){ memset(SendBuf, 0, PktSize); Sqn = Sqn + 1; memcpy(SendBuf, &Sqn, sizeof(int)); //allocate sequential number if (sendto(UDPSend, SendBuf, PktSize + 4, 0, (struct sockaddr *)&UDPAddr, slen) == SOCKET_ERROR){ printf("sendto() failed. Error code : %d\n", WSAGetLastError()); break; } else{ mtx_lock(&MutexS); Time = (double)UDPst.Elapsed() / 1000; //Elapsed time in second Pkts = Pkts + 1; ThreadRate = (double)Pkts * PktSize / (double)(Time * 1024 * 1024); //Transmission rate Pt->AggArray[ArrIndex] = ThreadRate; //Transmitting rate mtx_unlock(&MutexS); //Control rate if (ContrlRate != 0){ StdElapsed = (long double)Pkts * PktSize / ContrlRate; if (StdElapsed > Time){ SlpTime = 1000 * (StdElapsed - Time); Sleep(SlpTime); } } //Control Packet Number if (ContrlPkt != 0){ if (Pkts >= ContrlPkt){ printf("Transmission completed\n"); break; } } } } Pt->AggArray[ArrIndex] = 0; Pt->UDPNum = Pt->UDPNum - 1; //thrd_join(UDPChild, NULL); free(SendBuf); }
int TCPChild(void* Ptr){ struct Thread* PT = (struct Thread *)Ptr; //Initialize parameters int Recv = 0; int PktSize = 0; int ContrlRate = 0; int ContrlPkt = 0; int Send = 0; int ArrIndex = PT->Arr; long Pkts = 0; double Time = 0; double ThreadRate = 0; double StdElapsed = 0; double SlpTime = 0; SOCKET TCPSock; TCPSock = PT->TNewSock; mtx_lock(&MutexS); PT->TCPNum = PT->TCPNum + 1; //TCP client number mtx_unlock(&MutexS); char *ReceiveBuf; char *SendBuf; ReceiveBuf = (char *)malloc(sizeof(char)* 1024); while (1){ if ((Recv = recv(TCPSock, ReceiveBuf, 1024, 0)) == SOCKET_ERROR){ printf("recv() failed with error code: %d\n", WSAGetLastError()); return 0; } else{ memcpy(&PktSize, ReceiveBuf, sizeof(int)); memcpy(&ContrlRate, ReceiveBuf + 4, sizeof(int)); memcpy(&ContrlPkt, ReceiveBuf + 8, sizeof(int)); break; } } //Start a timer ES_FlashTimer TCPst = ES_FlashTimer(); TCPst.Start(); //Send data to client SendBuf = (char *)malloc(sizeof(char)* PktSize); while (1){ /* Insert data to Send buffer here*/ if ((Send = send(TCPSock, SendBuf, PktSize, 0)) == SOCKET_ERROR){ printf("\nConnection failed with error code: %d\n", WSAGetLastError()); break; } else{ mtx_lock(&MutexS); Time = (double)TCPst.Elapsed() / 1000; //Elapsed time in second Pkts = Pkts + 1; ThreadRate = (double)Pkts * PktSize / (double)(Time * 1024 * 1024); //Transmission rate PT->AggArray[ArrIndex] = ThreadRate; //Transmitting rate mtx_unlock(&MutexS); //Control rate if (ContrlRate != 0){ StdElapsed = (long double)Pkts * PktSize / ContrlRate; if (StdElapsed > Time){ SlpTime = 1000 * (StdElapsed - Time); Sleep(SlpTime); } } //Control Packet Number if (ContrlPkt != 0){ if (Pkts >= ContrlPkt){ printf("Transmission completed\n"); break; } } } } PT->TCPNum = PT->TCPNum - 1; PT->AggArray[ArrIndex] = 0; //thrd_join(TCPChild, NULL); free(ReceiveBuf); free(SendBuf); }
//print output stat void printStatData2(void* f) { int* freq = (int*) f; int sec; double r = 0; string unit; do{ #ifdef WIN32 EnterCriticalSection(&csec); #else //linux #endif r = sdata.currentRate; #ifdef WIN32 LeaveCriticalSection(&csec); sec = double(GetTickCount() - time_Begin)/1000.0; #else //linux sec = double(x_timer.Elapsed())/1000.0; #endif unit = "Bps"; if(r >= 1024) { r /= 1024; //KBps unit = "KBps"; } if (r >= 1024) { r /= 1024; //MBps; unit = "MBps"; } //cout << "Time: " << sec << "| Rate: " << r; printf("Elapsed [%ds] ",sec); #ifdef WIN32 EnterCriticalSection(&csec); #else //linux #endif cout << "Pkts [" << sdata.getrecvPkt() <<"] "; printf("Lost [%ld, %.4g%%] ", sdata.getlostPkt(), sdata.getlostRate()); printf("Rate [%.4g%s] ", r, unit.c_str()); printf("Jitter [%.4gms] ", sdata.calJitter(sdata.calltime)); #ifdef WIN32 LeaveCriticalSection(&csec); Sleep(*freq); #else //linux if(*freq >= 1000) { //cout << "sleep: " << (*freq)/1000 << "usleep: " << (*freq%1000)*1000 << endl; sleep((*freq)/1000); usleep((*freq%1000)*1000); } else { //cout << "usleep: " << (*freq)*1000 << endl; usleep((*freq)*1000); } #endif cout << '\r'; }while(*freq>=0); if(*freq<0) { cout << endl; exit(0); } return; }
//recv mode void recvInfo(RecvArg &r) { struct sockaddr_in peerAddr; SOCKET sockfd; int result; char recvbuf[RECV_BUF_SIZE]; DWORD tS, tC; double rate = 0; double bigT = 0.0; unsigned long long ttlByteRecv = 0; double time_prevRecv, time_currRecv; time_prevRecv = time_currRecv = 0; #ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); #else //linux #endif //make socket depends on protocal sockfd = socket(AF_INET, (r.protocal.compare("tcp")== 0 ? SOCK_STREAM : SOCK_DGRAM), IPPROTO_IP); //IPPROTO_IP if(sockfd == INVALID_SOCKET) { cout << "invalid socket: " << WSAGetLastError() << endl; WSACleanup(); return; } memset((void*) &peerAddr, '\0', sizeof(struct sockaddr_in)); peerAddr.sin_family = AF_INET; peerAddr.sin_addr.s_addr = inet_addr(r.host.c_str()); peerAddr.sin_port = htons(r.port); #ifdef WIN32 int addrlen = sizeof(struct sockaddr_in); #else socklen_t addrlen = sizeof(struct sockaddr_in); #endif result = bind(sockfd,(sockaddr*) &peerAddr, addrlen); if(result == SOCKET_ERROR) { cout << "cannot bind name to socket: " << WSAGetLastError() << endl; closesocket(sockfd); WSACleanup(); return; } // //set begin timer #ifdef WIN32 EnterCriticalSection(&csec); tS = time_Begin = GetTickCount(); LeaveCriticalSection(&csec); #else //linux tS = time_Begin = x_timer.ElapsedX(); #endif //conn // TCP =================================================== if(r.protocal.compare("tcp")==0) { listen(sockfd, MAX_LISTEN_SIZE); //backlog is 10 //check err cout << "wait for incoming message" << endl; SOCKET newSockfd = accept(sockfd,(sockaddr*) &peerAddr, &addrlen); //int i = -1; do { memset(recvbuf, '\0', sizeof(recvbuf)); result = recv(newSockfd, recvbuf, sizeof(recvbuf)-1, 0); #ifdef WIN32 //jitter time_currRecv = timeGetTime(); #else //linux time_currRecv = x_timer.ElapsedX(); #endif (time_prevRecv == 0)? (bigT = -1) : (bigT = time_currRecv - time_prevRecv); time_prevRecv = time_currRecv; if(result > 0) { ttlByteRecv += result; DWORD now; #ifdef WIN32 now = GetTickCount(); #else //linux now = x_timer.ElapsedX(); #endif rate = calRate(ttlByteRecv, double(now)/1000.0, double(tS)/1000.0); #ifdef WIN32 EnterCriticalSection(&csec); #else //linux #endif sdata.setStatData2( result, r.pktSize, 0, bigT, 1, rate); #ifdef WIN32 LeaveCriticalSection(&csec); #else //linux #endif } }while(result > 0); isDone = true; if(result <= 0) { closesocket(sockfd); WSACleanup(); return; } } // UDP =================================================== //unsigned long seqNum; if(r.protocal.compare("udp")== 0) { getsockname(sockfd, (SOCKADDR *)&peerAddr, &addrlen); struct sockaddr_in otherAddr; #ifdef WIN32 //set begin timer EnterCriticalSection(&csec); tS = time_Begin = GetTickCount(); LeaveCriticalSection(&csec); #else //linux tS = time_Begin = x_timer.ElapsedX(); #endif int seqNum; do { memset(recvbuf, '\0', sizeof(recvbuf)); result = recvfrom(sockfd, recvbuf, sizeof(recvbuf)-1, 0, (sockaddr*) &otherAddr, &addrlen); #ifdef WIN32 //jitter time_currRecv = timeGetTime(); #else //linux time_currRecv = x_timer.ElapsedX(); #endif (time_prevRecv == 0)? (bigT = -1) : (bigT = time_currRecv - time_prevRecv); time_prevRecv = time_currRecv; if(result > 0) { int prevSeqNum = seqNum; memcpy(&seqNum, recvbuf, sizeof(unsigned int)); if(prevSeqNum > seqNum) ttlByteRecv = 0; else ttlByteRecv += result; DWORD now; #ifdef WIN32 now = GetTickCount(); #else //linux now = x_timer.ElapsedX(); #endif rate = calRate(ttlByteRecv, double(now)/1000.0, double(tS)/1000.0); #ifdef WIN32 EnterCriticalSection(&csec); #else //linux #endif sdata.setStatData2( result, r.pktSize, sizeof(unsigned long), //header size bigT, seqNum, rate); #ifdef WIN32 LeaveCriticalSection(&csec); #else //linux #endif } }while(result > 0); isDone = true; if(result <= 0) { closesocket(sockfd); WSACleanup(); return; } } return; }
int main(int argc, char* argv[]) { isDone=false; char result = checkInput(argc, argv); int freq; //host information mode if(result == 'h') { HostInfo h; getHostInfo(argc, (argc<3)?"127.0.0.1":argv[2], h); printHostInfo(h); } //send mode if(result == 'x') //ori os 's' { SendArg s(argv); freq = s.freq; #ifdef WIN32 InitializeCriticalSectionAndSpinCount(&csec,0x00000400); _beginthread(printStatData2, 0, (void*) &freq); #else //linux tthread::thread t1(printStatData2, (void*) &freq); x_timer.Start(); #endif time_MainBegin = time(NULL); #ifdef WIN32 time_Begin = timeGetTime(); #else time_Begin = x_timer.ElapsedX(); #endif //cout << "send" << endl; sendInfo(s); #ifndef WIN32 //linux pthread_kill(t1.native_handle(), SIGINT); cout << "last Print" << endl; //t1.join(); #endif cout << '\r'; freq = -1; cout << "last Print" << endl; printStatData2(&freq); } //receive mode if(result == 'r') { RecvArg r(argv); freq = r.freq; #ifdef WIN32 InitializeCriticalSectionAndSpinCount(&csec,0x00000400); _beginthread(printStatData2, 0, (void*) &freq); #else //linux tthread::thread t1(printStatData2, (void*) &freq); x_timer.Start(); #endif time_MainBegin = time(NULL); recvInfo(r); #ifndef WIN32 //linux pthread_kill(t1.native_handle(), SIGINT); #endif cout << '\r'; freq = -1; printStatData2(&freq); } //Asg2 Server-Client if(result == 's') { //server mode time_serBegin = x_serTimer.Start(); ServerArg serArg(argv); int freq = serArg.freq; tthread::thread th(printServerData, (void*) freq); run_server(serArg); //printServerData(freq); th.join(); } if(result == 'c') { //client } // //system("Pause"); #ifdef WIN32 DeleteCriticalSection(&csec); #else //linux #endif return 0; }
//send mode void sendInfo(SendArg &s) { struct sockaddr_in peerAddr; SOCKET sockfd; int result; DWORD tS, tC; double r; unsigned long long ttlByteSent=0; double bigT = 0.0; unsigned long long ttlByteRecv = 0; double time_prevRecv, time_currRecv; time_prevRecv = time_currRecv = 0; #ifdef WIN32 WSADATA wsaData; result = WSAStartup(MAKEWORD(2,2), &wsaData); if(result != 0) { cout << "WSAStartup failed: " << WSAGetLastError() <<endl; return; } #else //linux #endif sockfd = socket(AF_INET, (s.protocal.compare("tcp")== 0 ? SOCK_STREAM : SOCK_DGRAM), IPPROTO_IP); if(sockfd == INVALID_SOCKET) { cout << "Fail to create socket: " << WSAGetLastError() << endl; } //set struct peerAddr.sin_addr.s_addr = inet_addr(s.host.c_str()); peerAddr.sin_family = AF_INET; peerAddr.sin_port = htons(s.port); //conn int addrlen = sizeof(struct sockaddr_in); if(s.protocal.compare("tcp")==0) { result = connect(sockfd, (struct sockaddr*) &peerAddr, addrlen); if(result < 0) { cout << "connection fail: " << WSAGetLastError() << endl; closesocket(sockfd); WSACleanup(); return; } } // //core #ifdef WIN32 EnterCriticalSection(&csec); tS = time_Begin = GetTickCount(); LeaveCriticalSection(&csec); #else //linux tS = time_Begin = x_timer.ElapsedX(); #endif char* sendbuf; unsigned int pktSize; sendbuf = createMessage(s.protocal, s.pktSize, s.pktNum, 1, pktSize); for(unsigned long i=1; i<=s.pktNum || s.pktNum==0; i++) { #ifdef WIN32 //jitter time_currRecv = timeGetTime(); #else //linux time_currRecv = x_timer.ElapsedX(); #endif (time_prevRecv == 0)? (bigT = -1) : (bigT = time_currRecv - time_prevRecv); time_prevRecv = time_currRecv; memcpy(sendbuf,&i,sizeof(unsigned long)); if(s.protocal.compare("tcp")==0) { result = send(sockfd, sendbuf, pktSize, 0); } else { //cout << "now sendto"; result = sendto(sockfd,sendbuf,pktSize, 0, (sockaddr*)&peerAddr, sizeof(peerAddr)); //cout << "..." << endl; } if(result == SOCKET_ERROR) { cout << "send failed: " << WSAGetLastError() << endl; WSACleanup(); return; } ttlByteSent += result; DWORD now; do { #ifdef WIN32 now = GetTickCount(); #else //linux now = x_timer.ElapsedX(); #endif //cout << "ttlByteSent: " <<ttlByteSent<<endl; r = calRate(ttlByteSent, double(now)/1000.0, double(tS)/1000.0); }while(r > s.rate && s.rate!=0); #ifdef WIN32 EnterCriticalSection(&csec); #else //linux #endif // << "data: {result=" << result << "} {pktSize=" << s.pktSize << "} rate=" << r << "}" << endl; sdata.setStatData2( result, s.pktSize, (s.protocal.compare("tcp")==0) ? 0 : sizeof(unsigned long), //ori: unsigned int bigT, //bigT (s.protocal.compare("tcp")==0) ? 1 : i, r); #ifdef WIN32 LeaveCriticalSection(&csec); #else //linux #endif } #ifdef WIN32 result = shutdown(sockfd, SD_SEND); if (result == SOCKET_ERROR) { cout << "cannot shutdown: " << WSAGetLastError() << endl; #endif closesocket(sockfd); #ifdef WIN32 WSACleanup(); } #else //cout << "send Finished" << endl; //pthread_kill(t.native_handle(), SIGINT); #endif isDone = true; return; }