#include #include #include #include #include #include #include #include #include #include #include #include #include int main (int argc, char** argv) { if (argc != 7) { cout << "timerS <# of tests> <# of bytes> <# of clients> \n"; cout << "\twhere socktype: \n\t\t1 -> SOCK_STREAM\n\t\t2 -> SOCK_CLUSTER\n"; cout << "\tThe starting port is ther port to which the first client connects, the nth client connects " << " to +(n-1)\n"; return 1; } struct ntptimeval start, stop; int socktype = atoi(argv[1]); if ((socktype <= 0) || (socktype > 2)) { cout << "Socktype must be 1 for SOCK_STREAM or 2 for SOCK_CLUSTER\n"; return 1; } int sockDeclVal = 0; switch(socktype) { case 1: sockDeclVal = SOCK_STREAM; break; case 2: sockDeclVal = SOCK_CLUSTER; break; } int numTests = atoi(argv[2]); int numBytes = atoi(argv[3]); int numClients = atoi(argv[4]); if (numClients < 1) { cout << "Must have at least one client\n"; return 1; } char* machineName = argv[5]; int startPort = atoi(argv[6]); int usecs = 0; int secs = 0; int* desc = new int[numClients]; int* newDesc = new int[numClients]; struct hostent* host = gethostbyname(machineName); for (int i=0; i < numClients; i++) { struct sockaddr_in sockStruct; sockStruct.sin_family = AF_INET; sockStruct.sin_port = startPort+i; sockStruct.sin_addr.s_addr = inet_addr(machineName); desc[i] = socket(PF_INET, sockDeclVal, 0); //0->ip if (desc[i] == -1) { cout << "Error setting protocol\n"; return 1; } if (bind(desc[i], (const sockaddr*)&sockStruct, sizeof(sockStruct)) == -1) { cout << "Error binding client, " << strerror(errno) << "\n"; return 1; } if (listen(desc[i], 0) == -1) { cout << "Error listening\n"; return 1; } struct sockaddr_in inSockStruct; socklen_t size; newDesc[i] = accept(desc[i], (sockaddr*)&inSockStruct, &size); if (newDesc[i] == -1) { cout << "Error accepting connection\n"; return 1; } } char response[1]; int sent = 0; char *sendMsg = new char[numBytes]; ntp_gettime(&start); //do the multiple times for (int j = 0; j < numTests; j++) { for (int k=0; k < numClients; k++) { if (sent = send(newDesc[k], (const void*) sendMsg, numBytes, MSG_WAITALL ) != numBytes) { cout << "Did not send all chars: " << sent << "\n"; return 1; } } for (int k=0; k < numClients; k++) { recv(newDesc[k], (void*)response, sizeof(response), MSG_WAITALL ); } } ntp_gettime(&stop); //compute how long it took secs = stop.time.tv_sec - start.time.tv_sec; if (stop.time.tv_usec < start.time.tv_usec) { //we didn't finish that last second secs--; usecs = 1000000 - (start.time.tv_usec - stop.time.tv_usec); } else { usecs = stop.time.tv_usec - start.time.tv_usec; } cout << "\nTest time secs: " << secs << " usecs: " << usecs << "\n"; //let the client disconnect sleep(4); for (int i=0; i < numClients; i++) { close(newDesc[i]); close(desc[i]); } delete sendMsg; delete desc; delete newDesc; return 0; };