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; }
//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; }