int main(int argc, char* argv[]) { //1 初始化Socket库 WSADATA wsa={0}; WSAStartup(MAKEWORD(2,2),&wsa); //2 使用(TCP/UDP通信)... UDPClient(); //3 卸载 WSACleanup(); return 0; }
int main(int argc, char **argv) { double ce=0.0, ab=0.0; struct timeval start, stop; int totalTime=0; //使用getopt来获取输入参数 int c; while ((c = getopt(argc, argv, "p:h:s:n:m:r:i:t:c:")) != EOF) { switch (c) { case 'i': printf("Set print mode: %d\n",atoi(optarg)); debug = atoi(optarg); break; case 'p': if (debug == 1) printf("INFO:udp portnumber: %d\n", atoi(optarg)); i_PortNumbUDP = atoi(optarg); break; case 'h': if (debug == 1) printf("INFO:hostname: %s\n", optarg); lp_ServerName = optarg; break; case 's': if (debug == 1) printf("INFO:packet size: %d\n", atoi(optarg)); i_PktSize = atoi(optarg); if (i_PktSize > MAX_PKT_SIZE) i_PktSize = MAX_PKT_SIZE; break; case 'n': if (debug == 1) printf("INFO:number of packet pair: %d\n", atoi(optarg)); i_PktNumbPP = atoi(optarg); if (i_PktNumbPP > MAX_PKT_NUM / 2) i_PktNumbPP = MAX_PKT_NUM / 2; break; case 'm': if (debug == 1) printf("INFO:length of packet train: %d\n", atoi(optarg)); i_PktNumbPT = atoi(optarg); if (i_PktNumbPT > MAX_PKT_NUM) i_PktNumbPT = MAX_PKT_NUM; break; case 'r': if (debug == 1) printf("INFO:overwrite packet train rate: %d\n", atoi(optarg)); i_rate = atoi(optarg); break; case 't': if (debug == 1) printf("INFO:overwirte test time: %d\n", atoi(optarg)); test_time = atoi(optarg); break; case 'c': if (debug == 1) printf("INFO:overwrite test count: %d\n", atoi(optarg)); test_count = atoi(optarg); break; case '?': printf("ERROR: illegal option %s\n", argv[optind-1]); printf("HELP: -p udp_portnumber -h hostname -s packet_size_bytes -n num_packet_pair -m train_length\n"); exit(1); break; default: printf("ERROR:no handler for option %c\n", c); if (debug == 1) printf("INFO:Usage: -p udp_portnumber -h hostname -s packet_size_bytes -n num_packet_pair -m train_length\n"); exit(1); break; } } // 处理ctrl-C事件,触发后执行CleanUp signal(SIGINT, CleanUp); // 初始化TCP、UDP TCPClient(lp_ServerName, i_PortNumbTCP); UDPClient(lp_ServerName, i_PortNumbUDP); // 确定时间精度 ProbeTimer(); for (test_i = 0; test_i<test_count; test_i++) { // 记录测量开始时间 gettimeofday(&start, NULL); //初始化测试时间与测试时长 initTestParam(); ce = PerformEst(PacketPair, i_PktNumbPP, 0); printf("--Ce = %.2f Mbps\n", ce ); ab = PerformEst (PacketTrain, i_PktNumbPT, ce); printf("--Ab = %.2f Mbps\n", ab); // 记录测量停止时间 gettimeofday(&stop, NULL); totalTime = (stop.tv_sec - start.tv_sec) * 1000000 + (stop.tv_usec - start.tv_usec); printf("--Time = %.3f Secs\n\n\n", (totalTime / (float)1000000)); if (test_time != -1 && test_i != test_count - 1) mySleep(interval - (totalTime / (float)1000000)); } CleanUp(0); return 0; } // end main()
int main(int argc, char *argv[]) { // Variables declarations and definitions int i; int enterOp2 = 0; int connectionType; int portNum; char hostname[128 + 1]; char buffer[128 + 1], operand1[128 + 1], operand2[128 + 1], opcode[128 + 1]; int op1Digit = 0, op2Digit = 0; // Let the user to enter the connection type printf("Enter the number of the connection type: (1) TCP or (2) UDP.\n"); printf(PROMPT); scanf("%d", &connectionType); // Check the user entered connection type while(connectionType > 2 || connectionType < 1) { printf("Error! Invalid connection type number!\n"); printf(PROMPT); scanf("%d", &connectionType); } // Let the user to enter the hostname printf("Enter the host name.\n"); printf(PROMPT); scanf("%s", hostname); // Check the host if(connectionType == 1) { server = gethostbyname(hostname); if (server == NULL) { perror("Error! No such host!\n"); exit(1); } } else if(connectionType == 2) { hp = gethostbyname(hostname); if (hp == NULL) { perror("Error! No such host!\n"); exit(1); } } // Let the user to enter the port number printf("Enter a port number.\n"); printf(PROMPT); scanf("%d", &portNum); // Check the user entered port number while(portNum < 0 || connectionType > 60000) { printf("Error! Invalid connection type number!\n"); printf(PROMPT); scanf("%d", &connectionType); } // Let the user to enter the opcode printf("Enter a opcode in binary of 7 digits\n"); printf("Enter 0000000 for +\n"); printf("Enter 0000001 for -\n"); printf("Enter 0000010 for *\n"); printf("Enter 0000011 for /\n"); printf(PROMPT); scanf("%s", opcode); // Validate the user input opcode while(strlen(opcode) != 7) { printf("Error! Invalid opcode.\n"); printf(PROMPT); scanf("%s", opcode); } for(;;) { if(strcmp(opcode, "0000000") == 0) { break; } else if(strcmp(opcode, "0000001") == 0) { break; } else if(strcmp(opcode, "0000010") == 0) { break; } else if(strcmp(opcode, "0000011") == 0) { break; } else { printf("Error! Invalid opcode.\n"); printf(PROMPT); scanf("%s", opcode); } } // Let the user to enter the first operand printf("Enter a binary number for the first operand.\n"); printf(PROMPT); scanf("%s", operand1); // Validate the user input first operand while(strlen(operand1) > 8) { printf("Error! Invalid first operand.\n"); printf(PROMPT); scanf("%s", operand1); } // Let the user to enter the second operand printf("Enter a binary number for the second operand.\n"); printf(PROMPT); scanf("%s", operand2); // Validate the user input first operand while(strlen(operand2) > 8) { printf("Error! Invalid second operand.\n"); printf(PROMPT); scanf("%s", operand2); } // Compose the packet // Version strcpy(buffer, "1"); // Type of Msg strcat(buffer, "0"); // Length of Msg strcat(buffer, "35"); // Operand 1 if(strlen(operand1) < OPERAND_DIGIT_SIZE) { for(i = 0; i < OPERAND_DIGIT_SIZE - strlen(operand1); i++) strcat(buffer, "0"); strcat(buffer, operand1); } else strcat(buffer, operand1); // Operand 2 if(strlen(operand2) < OPERAND_DIGIT_SIZE) { for(i = 0; i < OPERAND_DIGIT_SIZE - strlen(operand2); i++) strcat(buffer, "0"); strcat(buffer, operand2); } else strcat(buffer, operand2); // Opcode strcat(buffer, opcode); printf("%s\n", buffer); // If user input connection type equals 1, call TCPClient() function if(connectionType == 1) { TCPClient(portNum, server, buffer); } // Else if user input connection type equals 2, call UDPClient() function else if(connectionType == 2) { UDPClient(portNum, hp, buffer); } return 0; }