#include #include #include #include #include #include #include #include #include #include #include #define MAX_MSG 5 int main (int argc, char** argv) { if (argc != 5) { cout << "congestS <# of packets> \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 max = atoi(argv[2]); char* machineName = argv[3]; int startingPort = atoi(argv[4]); int usecs = 0; int secs = 0; int desc; int newDesc; struct hostent* host = gethostbyname(machineName); struct sockaddr_in sockStruct; sockStruct.sin_family = AF_INET; sockStruct.sin_port = startingPort; sockStruct.sin_addr.s_addr = inet_addr(machineName); desc = socket(PF_INET, sockDeclVal, 0); //0->ip if (desc == -1) { cout << "Error setting protocol\n"; return 1; } if (bind(desc, (const sockaddr*)&sockStruct, sizeof(sockStruct)) == -1) { cout << "Error binding client, " << strerror(errno) << "\n"; return 1; } if (listen(desc, 0) == -1) { cout << "Error listening\n"; return 1; } struct sockaddr_in inSockStruct; socklen_t size; newDesc = accept(desc, (sockaddr*)&inSockStruct, &size); if (newDesc == -1) { cout << "Error accepting connection\n"; return 1; } char* sendMsg = new char[MAX_MSG]; sendMsg[0] = 'A'; int finishedClients = 0; int numBytes = 0; char numBytesStr[5]; int sent; ntp_gettime(&start); for (int k=0; k < max; k++) { if (sent = send(newDesc, (const void*) sendMsg, 1, MSG_WAITALL ) != 1) { cout << "Did not send all chars, " << sent << "\n"; return 1; } if (sendMsg[0] != 'Z') sendMsg[0] += 1; else sendMsg[0] = 'A'; } 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"; cout << "Please continue the client, once the client is complete press enter\n"; cout.flush(); char msg[20]; cin.getline(msg, 20); //let clients close first sleep(3); close(newDesc); close(desc); delete sendMsg; return 0; };