进击的小白Day009——基于TCP的Socket编程(五)

还是这个程序,继续完善继续加功能。

贴代码:

/******************************************************************************
  filename: Connect.c
  author: renyuchao
  create date: 2018-10-29
  description:
  warning:
  modify history:
  Name      Date        description
******************************************************************************/
/*  includes */
#include "head.h"
/* local definitions */

/* local typedefs */

/* local functions declarations */

/* local variables declarations */

/* global variables declarations */


/*加,定义时间结构体*/
struct Time
{
	long int modeTime;
	long int connectTime;
	long int messageTime;
	long int allowTime[MAX_RUN];
	long int exeTime[MAX_RUN];
	long int calTime[MAX_RUN];
	long int waitTime[MAX_RUN];
	long int runTime[MAX_RUN];
}allTime;

/******************************************************************************
  function name:
  description:
  parameters: None
  ret_1urn Value:
  Error Code:

  warning: None
******************************************************************************/
int gettimeofday(struct timeval *tp, void *tzp)
{

	time_t clock;
	struct tm tm;
	SYSTEMTIME wtm;
	GetLocalTime(&wtm);
	tm.tm_year = wtm.wYear - 1900;
	tm.tm_mon = wtm.wMonth - 1;
	tm.tm_mday = wtm.wDay;
	tm.tm_hour = wtm.wHour;
	tm.tm_min = wtm.wMinute;
	tm.tm_sec = wtm.wSecond;
	tm.tm_isdst = -1;
	clock = mktime(&tm);
	tp->tv_sec = clock;
	tp->tv_usec = wtm.wMilliseconds * 1000;
	return 0;
}

/* function definitions */
/******************************************************************************
  function name:
  description:
  parameters: None
  ret_1urn Value:
  Error Code:

  warning: None
******************************************************************************/
int exe(void)
{
	long i = 10000000L;
	while (i--);
	//printf("	This is a program that needs to be executed !\n");
	return 0;
}


/* function definitions */
/******************************************************************************
  function name:
  description:
  parameters: None
  ret_1urn Value:
  Error Code:

  warning: None
******************************************************************************/
struct Time run(void)
{
	/*各种定义声明*/
	SOCKET server;
	SOCKET client;
	struct sockaddr_in serveraddr;
	struct sockaddr_in clientaddr;
	WSADATA wsaData;

	int len = sizeof(struct sockaddr_in);
	int exeTime, serverTime_i, clientTime_i, ret, ret_time_server, ret_time_client;
	int dec_1, sign_1, dec_2, sign_2;
	int t = T;
	int con;
	int mode, i;
	char receiveData[MAXDATA];
	char runData[MAXDATA];
	char byeData[MAXDATA];
	char serverTime_c[MAXDATA], *serverTime_c_server, clientTime_c[MAXDATA], *clientTime_c_client;
	double waitTime, serverTime_d, clientTime_d;
	clock_t startTime, endTime;  /*定义计时变量*/
	struct timeval start, end, allowstart, allowend, waitstart, waitend, exestart, exeend, calstart, calend;
	long int allowSum = 0, exeSum = 0, calSum = 0, waitSum = 0, runSum = 0;

	/*微妙计时开始*/
	gettimeofday(&start, NULL);

	/*选择服务端或客户端*/
	printf("Please choose server of client.		1.Server  2.Client\n");
	/*scanf("%d", &mode);
	getchar();*/
	mode = MODE;

	/*服务端*/
	if (mode == 1)
	{
		/*微妙计时结束,计算连接时间*/
		gettimeofday(&end, NULL);
		allTime.modeTime = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;

		/*微妙计时开始*/
		gettimeofday(&start, NULL);

		/*启动windows的socket服务,初始化WSA*/
		WSAStartup(0x101, &wsaData);

		/*创建服务端,初始化服务端地址*/
		memset((void*)&serveraddr, 0, sizeof(serveraddr));

		/*创建服务端套接字*/
		server = socket(AF_INET, SOCK_STREAM, 0);
		if (server == -1)
		{
			printf("Socket error !");
			getchar();
			exit(1);
		}

		/*绑定IP和端口*/
		serveraddr.sin_family = AF_INET;  /*定义家族协议*/
		serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");  /*定义主机地址*/
		serveraddr.sin_port = htons(PORT);  /*定义主机端口*/
		if (bind(server, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1)  /*绑定成功返回0*/
		{
			printf("Bind error !");
			getchar();
			exit(1);
		}

		/*开始监听*/
		if (listen(server, 20) == -1)
		{
			printf("Listen error !");
			getchar();
			exit(1);
		}

		/*建立连接*/
		//printf("Waiting for connection...\n\n");
		client = accept(server, (SOCKADDR *)&clientaddr, &len);
		if (client == -1)
		{
			printf("Accept error !");
			getchar();
			exit(1);
		}
		//printf("Receive a connection: %s \r\n\n", inet_ntoa(clientaddr.sin_addr));

		/*微妙计时结束,计算连接时间*/
		gettimeofday(&end, NULL);
		allTime.connectTime = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;

		/*微妙计时开始*/
		gettimeofday(&start, NULL);

		for (i = 0; i < MAX_MESSAGE; i++)
		{
			/*发送准备信息*/
			if (send(client, "I'm ready !", 11, 0) < 0)
			{
				printf("\n\nSend error or connection interruption !\n");
				getchar();
				exit(1);
			}

			/*接收准备信息*/
			ret = recv(client, receiveData, MAXDATA, 0);
			if (ret > 0)
			{
				receiveData[ret] = 0x00;
				//printf("From client %s: %s\n",inet_ntoa(clientaddr.sin_addr), receiveData);
			}
			/*接收不到客户端数据时中断循环*/
			else
			{
				printf("\n\nReceive error or connection interruption !\n");
				getchar();
				exit(1);
			}
		}

		/*微妙计时结束,计算消息时间*/
		gettimeofday(&end, NULL);
		allTime.messageTime = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;

		for (i = 0; i < MAX_RUN; i++)
		{
			/*微妙计时开始*/
			gettimeofday(&start, NULL);

			/*微妙计时开始*/
			gettimeofday(&allowstart, NULL);

			/*发:第一次握手*/
			if (send(client, "First:Run !", 11, 0) < 0)
			{
				printf("\n\nSend error or connection interruption !\n");
				getchar();
				exit(1);
			}

			/*收:第二次握手*/
			ret = recv(client, runData, MAXDATA, 0);
			if (ret > 0)
			{
				runData[ret] = 0x00;
			}
			else
			{
				printf("\n\nReceive error or connection interruption !\n");
				getchar();
				exit(1);
			}

			if (strcmp(runData, "Second:Run !") == 0)
			{
				/*发:第三次握手*/
				if (send(client, "Third:Run !", 11, 0) < 0)
				{
					printf("\n\nSend error or connection interruption !\n");
					getchar();
					exit(1);
				}

				/*微妙计时结束,计算运行时间*/
				gettimeofday(&allowend, NULL);
				allTime.allowTime[i] = 1000000 * (allowend.tv_sec - allowstart.tv_sec) + allowend.tv_usec - allowstart.tv_usec;
				allowSum = allowSum + allTime.allowTime[i];

				/*运行时间大于周期时间,则直接运行运行*/
				if (t >= T)
				{
					/*微妙计时开始*/
					gettimeofday(&exestart, NULL);

					/*计时开始*/
					startTime = clock();

					exe();

					/*计时结束*/
					endTime = clock();

					/*微妙计时结束,计算运行时间*/
					gettimeofday(&exeend, NULL);
					allTime.exeTime[i] = 1000000 * (exeend.tv_sec - exestart.tv_sec) + exeend.tv_usec - exestart.tv_usec;
					exeSum = exeSum + allTime.exeTime[i];

					/*微妙计时开始*/
					gettimeofday(&calstart, NULL);

					/*服务端运行用时*/
					serverTime_d = (double)(endTime - startTime);
					/*printf("serverTime_d=%ld\n", serverTime_d);
					printf("exeTime=%ld\n", allTime.exeTime[i] / 1000);*/

					/*服务端运行用时转字符串*/
					ecvt(serverTime_d, 10, &dec_1, &sign_1);
					serverTime_c_server = ecvt(serverTime_d, dec_1, &dec_2, &sign_2);

					/*发送客户端运行用时*/
					if (send(client, serverTime_c_server, strlen(serverTime_c_server), 0) < 0)
					{
						printf("\n\nSend error or connection interruption !\n");
						getchar();
						exit(1);
					}

					/*接收客户端运行用时*/
					ret_time_client = recv(client, clientTime_c, MAXDATA, 0);
					if (ret_time_client > 0)
					{
						clientTime_c[ret_time_client] = 0x00;
					}
					/*接收不到客户端数据时中断循环*/
					else
					{
						printf("\n\nReceive error or connection interruption !\n");
						getchar();
						exit(1);
					}

					/*字符串转整形*/
					serverTime_i = atoi(serverTime_c_server);
					//printf("Server's operation time is %d\n", serverTime_i);
					clientTime_i = atoi(clientTime_c);
					//printf("Client's operation time is %d\n", clientTime_i);

					/*比较服务端、客户端用时长短*/
					if (serverTime_i >= clientTime_i)
					{
						exeTime = serverTime_i;
					}
					else
					{
						exeTime = clientTime_i;
					}

					t = exeTime;

					//printf("The whole system's operation time is %d\n\n", t);

					/*微妙计时结束,计算运行时间*/
					gettimeofday(&calend, NULL);
					allTime.calTime[i] = 1000000 * (calend.tv_sec - calstart.tv_sec) + calend.tv_usec - calstart.tv_usec;
					calSum = calSum + allTime.calTime[i];

					/*运行总时长小于周期时间,挂起*/
					if (t <= T)
					{
						/*微妙计时开始*/
						gettimeofday(&waitstart, NULL);

						waitTime = T - t - (allTime.calTime[i] + allTime.allowTime[i]) / 1000;
						//waitTime = T - t;
						Sleep(waitTime);
						t = T;

						/*微妙计时结束,计算运行时间*/
						gettimeofday(&waitend, NULL);
						allTime.waitTime[i] = 1000000 * (waitend.tv_sec - waitstart.tv_sec) + waitend.tv_usec - waitstart.tv_usec;
						waitSum = waitSum + allTime.waitTime[i];
					}
					else
					{
						allTime.waitTime[i] = 0;
					}
				}
			}

			/*微妙计时结束,计算运行时间*/
			gettimeofday(&end, NULL);
			allTime.runTime[i] = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
			runSum = runSum + allTime.runTime[i];
		}

		///*发:第一次挥手*/
		//if (send(client, "First:Bye !", 11, 0) < 0)
		//{
		//	printf("\n\nSend error or connection interruption !\n");
		//	getchar();
		//	exit(1);
		//}

		///*收:第二次挥手*/
		//ret = recv(client, runData, MAXDATA, 0);
		//if (ret > 0)
		//{
		//	runData[ret] = 0x00;
		//}
		//else
		//{
		//	printf("\n\nReceive error or connection interruption !\n");
		//	getchar();
		//	exit(1);
		//}
		//
		///*收:第三次挥手*/
		//ret = recv(client, runData, MAXDATA, 0);
		//if (ret > 0)
		//{
		//	runData[ret] = 0x00;
		//}
		//else
		//{
		//	printf("\n\nReceive error or connection interruption !\n");
		//	getchar();
		//	exit(1);
		//}
		//
		//if (strcmp(runData, "Third:Bye !") == 0)
		//{
		//	/*发:第四次挥手*/
		//	if (send(client, "Forth:Bye !", 11, 0) < 0)
		//	{
		//		printf("\n\nSend error or connection interruption !\n");
		//		getchar();
		//		exit(1);
		//	}
		//}

		closesocket(server);
		WSACleanup();

		printf("modeTime=%ld, connectTime=%ld, messageTime=%ld\n", allTime.modeTime, allTime.connectTime, allTime.messageTime);
		printf("allowTime=%ld, exeTime=%ld, calTime=%ld, waitTime=%ld\n\n", allowSum / MAX_RUN, exeSum / MAX_RUN, calSum / MAX_RUN, waitSum / MAX_RUN);
		//printf("runTime=%ld\n\n", runSum / MAX_RUN);

	}

	/*客户端*/
	else
	{
	/*微妙计时结束,计算连接时间*/
	gettimeofday(&end, NULL);
	allTime.modeTime = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;

	/*微妙计时开始*/
	gettimeofday(&start, NULL);

	/*启动windows的socket服务,初始化WSA*/
	WSAStartup(0x101, &wsaData);

	/*创建客户端,初始化客户端地址*/
	memset((void*)&clientaddr, 0, sizeof(clientaddr));

	/*创建客户端套接字*/
	client = socket(AF_INET, SOCK_STREAM, 0);
	if (client == -1)
	{
		printf("Socket error !");
		getchar();
		exit(1);
	}

	/*绑定IP和端口*/
	clientaddr.sin_family = AF_INET;  /*定义家族协议*/
	clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  /*定义主机地址*/
	clientaddr.sin_port = htons(PORT);  /*定义主机端口*/

	/*连接服务端*/
	con = connect(client, (struct sockaddr*)&clientaddr, sizeof(clientaddr));

	/*微妙计时结束,计算连接时间*/
	gettimeofday(&end, NULL);
	allTime.connectTime = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;

	/*微妙计时开始*/
	gettimeofday(&start, NULL);

	for (i = 0; i < MAX_MESSAGE; i++)
	{
		/*接收准备信息*/
		ret = recv(client, receiveData, MAXDATA, 0);
		if (ret > 0)
		{
			receiveData[ret] = 0x00;
			//printf("From server: %s\n", receiveData);
		}
		/*接收不到客户端数据时中断循环*/
		else
		{
			printf("\n\nReceive error or connection interruption !\n");
			getchar();
			exit(1);
		}

		/*发送准备信息*/
		if (send(client, "I'm ready too !", 15, 0) < 0)
		{
			printf("\n\nSend error or connection interruption !\n");
			getchar();
			exit(1);
		}
	}

	/*微妙计时结束,计算消息时间*/
	gettimeofday(&end, NULL);
	allTime.messageTime = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;

	/*检验运行许可指令并开始执行*/

	for (i = 0; i < MAX_RUN; i++)  /*改*/
	{
		/*微妙计时开始*/
		gettimeofday(&start, NULL);

		/*微妙计时开始*/
		gettimeofday(&allowstart, NULL);

		/*收:第一次握手*/
		ret = recv(client, runData, MAXDATA, 0);
		if (ret > 0)
		{
			runData[ret] = 0x00;
		}
		else
		{
			printf("\n\nReceive error or connection interruption !\n");
			getchar();
			exit(1);
		}

		if (strcmp(runData, "First:Run !") == 0)
		{
			/*发:第二次握手*/
			if (send(client, "Second:Run !", 12, 0) < 0)
			{
				printf("\n\nSend error or connection interruption !\n");
				getchar();
				exit(1);
			}
		}

		/*收:第三次握手*/
		ret = recv(client, runData, MAXDATA, 0);
		if (ret > 0)
		{
			runData[ret] = 0x00;
		}
		else
		{
			printf("\n\nReceive error or connection interruption !\n");
			getchar();
			exit(1);
		}

		/*微妙计时结束,计算运行时间*/
		gettimeofday(&allowend, NULL);
		allTime.allowTime[i] = 1000000 * (allowend.tv_sec - allowstart.tv_sec) + allowend.tv_usec - allowstart.tv_usec;
		allowSum = allowSum + allTime.allowTime[i];

		if (strcmp(runData, "Third:Run !") == 0)
		{
			/*运行时间大于周期时间,则直接运行运行*/
			if (t >= T)
			{
				/*微妙计时开始*/
				gettimeofday(&exestart, NULL);

				/*计时开始*/
				startTime = clock();

				exe();

				/*计时结束*/
				endTime = clock();

				/*微妙计时结束,计算运行时间*/
				gettimeofday(&exeend, NULL);
				allTime.exeTime[i] = 1000000 * (exeend.tv_sec - exestart.tv_sec) + exeend.tv_usec - exestart.tv_usec;
				exeSum = exeSum + allTime.exeTime[i];

				/*微妙计时开始*/
				gettimeofday(&calstart, NULL);

				/*客户端运行用时*/
				clientTime_d = (double)(endTime - startTime);

				/*客户端运行用时转字符串*/
				ecvt(clientTime_d, 10, &dec_1, &sign_1);
				clientTime_c_client = ecvt(clientTime_d, dec_1, &dec_2, &sign_2);

				/*发送客户端运行用时*/
				if (send(client, clientTime_c_client, strlen(clientTime_c_client), 0) < 0)
				{
					printf("\n\nSend error or connection interruption !\n");
					getchar();
					exit(1);
				}

				/*接收客户端运行用时*/
				ret_time_server = recv(client, serverTime_c, MAXDATA, 0);
				if (ret_time_server > 0)
				{
					serverTime_c[ret_time_server] = 0x00;
				}
				/*接收不到客户端数据时中断循环*/
				else
				{
					printf("\n\nReceive error or connection interruption !\n");
					getchar();
					exit(1);
				}

				/*字符串转整形*/
				serverTime_i = atoi(serverTime_c);
				//printf("Server's operation time is %d\n", serverTime_i);
				clientTime_i = atoi(clientTime_c_client);
				//printf("Client's operation time is %d\n", clientTime_i);

				/*比较服务端、客户端用时长短*/
				if (serverTime_i >= clientTime_i)
				{
					exeTime = serverTime_i;
				}
				else
				{
					exeTime = clientTime_i;
				}

				t = exeTime;

				//printf("The whole system's operation time is %d\n\n", t);

				/*微妙计时结束,计算运行时间*/
				gettimeofday(&calend, NULL);
				allTime.calTime[i] = 1000000 * (calend.tv_sec - calstart.tv_sec) + calend.tv_usec - calstart.tv_usec;
				calSum = calSum + allTime.calTime[i];

				/*运行总时长小于周期时间,挂起*/
				if (t <= T)
				{
					/*微妙计时开始*/
					gettimeofday(&waitstart, NULL);

					waitTime = T - t - (allTime.calTime[i] + allTime.allowTime[i]) / 1000;
					//waitTime = T - t;
					Sleep(waitTime);
					t = T;

					/*微妙计时结束,计算运行时间*/
					gettimeofday(&waitend, NULL);
					allTime.waitTime[i] = 1000000 * (waitend.tv_sec - waitstart.tv_sec) + waitend.tv_usec - waitstart.tv_usec;
					waitSum = waitSum + allTime.waitTime[i];
				}
				else
				{
					allTime.waitTime[i] = 0;
				}
			}
		}
		/*微妙计时结束,计算运行时间*/
		gettimeofday(&end, NULL);
		allTime.runTime[i] = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
		runSum = runSum + allTime.runTime[i];
	}

	///*收:第一次挥手*/
	//ret = recv(client, runData, MAXDATA, 0);
	//if (ret > 0)
	//{
	//	runData[ret] = 0x00;
	//}
	//else
	//{
	//	printf("\n\nReceive error or connection interruption !\n");
	//	getchar();
	//	exit(1);
	//}

	//if (strcmp(runData, "First:Bye !") == 0)
	//{
	//	/*发:第二次挥手*/
	//	if (send(client, "Second:Bye !", 12, 0) < 0)
	//	{
	//		printf("\n\nSend error or connection interruption !\n");
	//		getchar();
	//		exit(1);
	//	}
	//}

	///*发:第三次挥手*/
	//if (send(client, "Third:Bye !", 11, 0) < 0)
	//{
	//	printf("\n\nSend error or connection interruption !\n");
	//	getchar();
	//	exit(1);
	//}

	///*收:第四次挥手*/
	//ret = recv(client, runData, MAXDATA, 0);
	//if (ret > 0)
	//{
	//	runData[ret] = 0x00;
	//}
	//else
	//{
	//	printf("\n\nReceive error or connection interruption !\n");
	//	getchar();
	//	exit(1);
	//}

	//if (strcmp(runData, "Forth:Bye !") == 0)
	//{
	//	closesocket(client);
	//	WSACleanup();
	//}

	printf("modeTime=%ld, connectTime=%ld, messageTime=%ld\n", allTime.modeTime, allTime.connectTime, allTime.messageTime);
	printf("allowTime=%ld, exeTime=%ld, calTime=%ld, waitTime=%ld\n\n", allowSum / MAX_RUN, exeSum / MAX_RUN, calSum / MAX_RUN, waitSum / MAX_RUN);
	//printf("runTime=%ld\n\n", runSum / MAX_RUN);
	}
	return allTime;

}

int main(void)
{
	/*各种定义*/
	struct Time pTime[MAX_MAIN];
	int i, j, k, l;
	long int modeTimeMax, connectTimeMax, messageTimeMax, allowTimeMax, exeTimeMax, calTimeMax, waitTimeMax, runTimeMax;
	long int modeTimeSum, connectTimeSum, messageTimeSum, allowTimeSum, exeTimeSum, calTimeSum, waitTimeSum, runTimeSum;
	long int modeTimeAve, connectTimeAve, messageTimeAve, allowTimeAve, exeTimeAve, calTimeAve, waitTimeAve, runTimeAve;

	/*运行程序*/
	for (i = 0; i < MAX_MAIN; i++)
	{
		pTime[i] = run();
	}

	/*计算选择模式的最大时间*/
	for (j = 0, modeTimeMax = 0; j < MAX_MAIN; j++)
	{
		if (modeTimeMax < pTime[j].modeTime)
		{
			modeTimeMax = pTime[j].modeTime;
		}
	}

	/*计算连接的最大时间*/
	for (j = 0, connectTimeMax = 0; j < MAX_MAIN; j++)
	{
		if (connectTimeMax < pTime[j].connectTime)
		{
			connectTimeMax = pTime[j].connectTime;
		}
	}

	/*计算发送接收消息的最大时间*/
	for (j = 0, messageTimeMax = 0; j < MAX_MAIN; j++)
	{
		if (messageTimeMax < pTime[j].messageTime)
		{
			messageTimeMax = pTime[j].messageTime;
		}
	}

	/*计算三次握手的最大时间*/
	for (k = 0, allowTimeMax = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			if (allowTimeMax < pTime[k].allowTime[l])
			{
				allowTimeMax = pTime[k].allowTime[l];
			}
		}
	}

	/*计算执行算法的最大时间*/
	for (k = 0, exeTimeMax = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			if (exeTimeMax < pTime[k].exeTime[l])
			{
				exeTimeMax = pTime[k].exeTime[l];
			}
		}
	}

	/*计算时间汇总的最大时间*/
	for (k = 0, calTimeMax = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			if (calTimeMax < pTime[k].calTime[l])
			{
				calTimeMax = pTime[k].calTime[l];
			}
		}
	}

	/*计算等待的最大时间*/
	for (k = 0, waitTimeMax = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			if (waitTimeMax < pTime[k].waitTime[l])
			{
				waitTimeMax = pTime[k].waitTime[l];
			}
		}
	}

	/*计算循环的最大时间*/
	for (k = 0, runTimeMax = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			if (runTimeMax < pTime[k].runTime[l])
			{
				runTimeMax = pTime[k].runTime[l];
			}
		}
	}

	/*计算选择模式的平均时间*/
	for (j = 0, modeTimeSum = 0; j < MAX_MAIN; j++)
	{
		modeTimeSum += pTime[j].modeTime;
	}
	modeTimeAve = modeTimeSum / MAX_MAIN;

	/*计算连接的平均时间*/
	for (j = 0, connectTimeSum = 0; j < MAX_MAIN; j++)
	{
		connectTimeSum += pTime[j].connectTime;
	}
	connectTimeAve = connectTimeSum / MAX_MAIN;

	/*计算发送接收消息的平均时间*/
	for (j = 0, messageTimeSum = 0; j < MAX_MAIN; j++)
	{
		messageTimeSum += pTime[j].messageTime;
	}
	messageTimeAve = messageTimeSum / MAX_MAIN;

	/*计算执行算法的平均时间*/
	for (k = 0, allowTimeSum = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			allowTimeSum += pTime[k].allowTime[l];
		}
	}
	allowTimeAve = allowTimeSum / (MAX_MAIN * MAX_RUN);

	/*计算执行算法的平均时间*/
	for (k = 0, exeTimeSum = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			exeTimeSum += pTime[k].exeTime[l];
		}
	}
	exeTimeAve = exeTimeSum / (MAX_MAIN * MAX_RUN);

	/*计算时间汇总的平均时间*/
	for (k = 0, calTimeSum = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			calTimeSum += pTime[k].calTime[l];
		}
	}
	calTimeAve = calTimeSum / (MAX_MAIN * MAX_RUN);

	/*计算等待的平均时间*/
	for (k = 0, waitTimeSum = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			waitTimeSum += pTime[k].waitTime[l];
		}
	}
	waitTimeAve = waitTimeSum / (MAX_MAIN * MAX_RUN);

	/*计算循环的平均时间*/
	for (k = 0, runTimeSum = 0; k < MAX_MAIN; k++)
	{
		for (l = 0; l < MAX_RUN; l++)
		{
			runTimeSum += pTime[k].runTime[l];
		}
	}
	runTimeAve = runTimeSum / (MAX_MAIN * MAX_RUN);

	/*输出结果*/
	printf("modeTimeMax=%ld, modeTimeAve=%ld\n", modeTimeMax, modeTimeAve);
	printf("connectTimeMax=%ld, connectTimeAve=%ld\n", connectTimeMax, connectTimeAve);
	printf("messageTimeMax=%ld, messageTimeAve=%ld\n", messageTimeMax, messageTimeAve);
	printf("allowTimeMax=%ld, allowTimeAve=%ld\n", allowTimeMax, allowTimeAve);
	printf("exeTimeMax=%ld, exeTimeAve=%ld\n", exeTimeMax, exeTimeAve);
	printf("calTimeMax=%ld, calTimeAve=%ld\n", calTimeMax, calTimeAve);
	printf("waitTimeMax=%ld, waitTimeAve=%ld\n", waitTimeMax, waitTimeAve);
	//printf("runTimeMax=%ld, runTimeAve=%ld\n", runTimeMax, runTimeAve);

	Sleep(10000000000);

	return 0;
}

收获:

  1. 单步的时候,遇到scanf会停止,可以在scanf后面加上getchar,就可以正常单步

最近事情好多啊,不知道自己还能坚持多久,有时候一个程序在一个地方就能卡一天,而我现在并没有太多自己去研究去扣的时间,有点儿扎心,有点儿心累,不知道自己还能坚持多久。

猜你喜欢

转载自blog.csdn.net/u011232393/article/details/83961045