⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socktest.c

📁 由smsc公司改进的lwip2.1.1基于嵌入式系统的TCP/ip协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
		} while (1);

		/* Record end time */
		read_end_time(test);

		SMSC_TRACE(1,("Data send done..."));
        
		end_of_transmission(test);
	}

	/* receive data */
	if (!test->sflag)
	{
		if ((msgbuf = malloc(MAX_RECV_MSG_SIZE)) == NULL)
		{
			SMSC_NOTICE(1, ("malloc error.."));
			return 1;
		}
		otherAddrLen = sizeof(test->otherAddr);
#if defined (UNIX) || defined (WIN32)
		/* install signal handler */
		signal(SIGINT, (void *)catch_signal);
#endif
		/* Record the start time */
		read_start_time(test);	
		
		/* receive message */
		do {
			memset(msgbuf, 0, MAX_RECV_MSG_SIZE);

			if ((rc = msgrecv(test, msgbuf, MAX_RECV_MSG_SIZE)) < 0) {
				SMSC_NOTICE(1, ("msgrecv data failure.."));
				return 1;
			}

			if (strcmp((char *)msgbuf, START_MARKER) == 0) {	
				SMSC_NOTICE(1, ("Got the START marker .."));

				/* Restart the record start time */
				read_start_time(test);	

				/* discard the extra bytes read */ 
				test->dataCount += (rc - strlen(START_MARKER));
				continue;
			}

			if (strcmp((char *)msgbuf, END_MARKER) == 0) {	
				SMSC_NOTICE(1,("Got the END marker .."));

				/* record end time */
				read_end_time(test);

				stat.bytesReceived = test->byteCount;
				stat.secRecvTime = test->realTime;

				/* SMSC_NOTICE(1,("stat.bytesReceived=%d, stat.secRecvTime=%lu",stat.bytesReceived, (unsigned long)stat.secRecvTime-4)); */
				
				/* Send no. of bytes received*/
				if (msgsend(test, (char *)&stat.bytesReceived, sizeof(stat.bytesReceived)) < 0) {
					SMSC_NOTICE(1, ("send data failure.."));
					return 1;
				}	
				if (msgsend(test, (char *)&stat.secRecvTime, sizeof(stat.secRecvTime)) < 0) {
					SMSC_NOTICE(1, ("send data failure.."));
					return 1;
				}	
				break;
			}
				
			if (!test->skipVerify) {
				/* Verify integrity of the data */
				if (verify_data(test, msgbuf, rc) != 0)
					return 1;
			}
			test->msgCount++;
			test->byteCount += rc;
			/* if (test->count)
				if (!(--test->count))
					break;
			*/
		} while (1);

		SMSC_NOTICE(1, ("Bytes received: %d  Time to receive:=%f", test->byteCount, test->realTime));
		SMSC_TRACE(1, ("Throughput: %lu Kbps", (unsigned long)((stat.bytesReceived * 8.0)/test->realTime/1000.0)));
	}

#ifdef WIN32
	closesocket(test->nsd);
#else
	close(test->nsd);
#endif

	return 0;

}

int msgsend(struct tester *test, char *msgbuf, unsigned int msglen)
{
	int count,sd;
#if SELECT_TEST
	fd_set write_fd;
#endif
		
	if (test->udp)
		sd=test->sd;
	else
		sd=test->sflag ? test->sd : test->nsd;
		
#if SELECT_TEST	
	/* To exercise select */
	FD_ZERO(&write_fd);
	FD_SET(sd, &write_fd);
	SMSC_NOTICE(1,("msgsend select(): Waiting on write descriptors"));
	if ((select(sd + 1, NULL, &write_fd, NULL, NULL)) == SOCKET_ERROR) {
		SMSC_NOTICE(1, ("select error..."));
		return 1;
	}
	SMSC_NOTICE(1,("select(): Waiting on done %d",FD_ISSET(sd,&write_fd)));
#endif

	if (test->udp) {

again:
		if ((count = sendto(sd, msgbuf, msglen, 0, 
			(struct sockaddr *) &test->otherAddr, sizeof(struct sockaddr_in))) <= 0) {
#ifdef UNIX
			if (errno == ENOBUFS) {
				usleep(10000);
				errno = 0;
#else 
			if (count == 0 || errno == ENOBUFS) {
#ifdef WIN32
			Sleep(1);
#else
			smsc_mdelay(1);	/* tune this value for better throughput */			
#endif
#endif
				/* SMSC_NOTICE(1, ("msgsend(): Not able to send data, re-trying..")); */
				goto again;
			}
			SMSC_NOTICE(1, ("send to error.."));
			return -1;
		}
	}
	else {
	
tryagain:
		if ((count = send(sd, msgbuf, msglen, 0)) <= 0) {
			if (count == 0) {
#ifdef UNIX
			usleep(100);			
#else
#ifdef WIN32
			Sleep(1);
#else 
			smsc_mdelay(1);	/* tune this value for better throughput */			
#endif
#endif
				/* SMSC_NOTICE(1, ("msgsend(): Not able to send data, re-trying..")); */
				goto tryagain;
			}
			SMSC_NOTICE(1, ("send error.."));
			return -1;
		}
	}
	return count;
}

int msgrecv(struct tester *test, char *msgbuf, unsigned int msglen)
{                                                
	int count,sd;
	int otherAddrLen = sizeof(test->otherAddr);
#if SELECT_TEST
	fd_set read_fd;
	struct timeval time_val; 
#endif
	
	if (test->udp)
		sd=test->sd;
	else
		sd=test->sflag ? test->sd : test->nsd;
	
#if SELECT_TEST	
	/* To exercise select */
#if 0
	/* polling: (set the 0 timeout)*/
retry:
	FD_ZERO(&read_fd);
	FD_SET(sd, &read_fd);
	time_val.tv_sec=0;
	time_val.tv_usec=0;
	SMSC_NOTICE(1,("msgrecv select(): polling on read descriptors"));	
	if ((select(sd + 1, &read_fd, NULL, NULL, &time_val)) == SOCKET_ERROR) {
			SMSC_NOTICE(1, ("select error..."));
			return 1;
	}
	SMSC_NOTICE(1,("select(): polling done %d", FD_ISSET(sd,&read_fd)));
#ifdef WIN32
				Sleep(1000);
#else
#ifdef UNIX
				sleep(1);
#else
				smsc_mdelay(1000);
#endif	
#endif
	if (!(FD_ISSET(sd,&read_fd)))
		goto retry;
#else
	/* Checking if any socket is ready (without timeout) */
	FD_ZERO(&read_fd);
	FD_SET(sd, &read_fd);
	SMSC_NOTICE(1,("msgrecv select(): Waiting on read descriptors"));
	if ((select(sd + 1, &read_fd, NULL, NULL, NULL)) == SOCKET_ERROR) {
			SMSC_NOTICE(1, ("select error..."));
			return 1;
	}
	SMSC_NOTICE(1,("select(): Waiting on done %d", FD_ISSET(sd,&read_fd)));
#endif
#endif

	if (test->udp)
	{
		if ((count = recvfrom(sd, (void *)msgbuf, msglen, 0,
			(struct sockaddr *) &test->otherAddr, (int *)&otherAddrLen)) < 0) {
			SMSC_NOTICE(1, ("recevfrom error.."));
			return -1;
		}
	}	
	else {
		if ((count = recv(sd, msgbuf, msglen, 0)) < 0) {
			SMSC_NOTICE(1,("read error.."));
			return -1;
		}
	}
	return count;
}

void read_start_time(struct tester *test)
{
	SMSC_TRACE(1,("Recording start time.."));
#ifdef WIN32
	test->startTime = GetTickCount();
#else
#ifdef UNIX
	gettimeofday(&test->startTime, (struct timezone *)0);
#else
	test->startTime = smsc_time_now();
#endif
#endif
}

void read_end_time(struct tester *test)
{
#ifdef WIN32
	double endtime;
	endtime = GetTickCount();
	test->realTime = ((double) endtime - (double) test->startTime)/1000;
#else
#ifdef UNIX
	struct timeval endtime, tdiff;
	gettimeofday(&endtime, (struct timezone *)0);		
	tvsub(&tdiff, &endtime, &test->startTime);
	test->realTime = tdiff.tv_sec + ((double)tdiff.tv_usec) / 1000000;
#else
	/*test->realTime = (double) time_minus(smsc_time_now(),test->startTime)/time_ticks_per_sec();*/
	test->realTime = (double) smsc_tick_to_sec(smsc_time_minus(smsc_time_now(), test->startTime));
#endif
#endif	
	SMSC_TRACE(1,("Record END time"));
}

#ifdef UNIX
void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0)
{
	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
	if (tdiff->tv_usec < 0)
		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}
#endif

void end_of_transmission(struct tester *test)
{
	int count, otherAddrLen;
	struct stat stat;

	/* Slow server may be still receiving data..wait..*/
	SMSC_TRACE(1,("Waiting for 4 sec before sending end marker"));
#ifdef WIN32
	Sleep(4000);
#else
#ifdef UNIX
	sleep(4);
#else
	smsc_mdelay(4000);
#endif
#endif
	memset(&stat, 0, sizeof(stat));

	SMSC_TRACE(1,("Sending END_MARKER"));

	/* Send end marker */
	if (msgsend(test, END_MARKER, strlen(END_MARKER)) < 0) {
		SMSC_NOTICE(1, ("send data failure.."));
		return;
	}	

	SMSC_TRACE(1,("Waiting for stat response..."));

	/* Receive response */	
	otherAddrLen = sizeof(test->otherAddr);
	if (test->udp) {
		if ((count = recvfrom(test->sd, (char *)&stat, sizeof(stat), 0,
			(struct sockaddr *) &test->otherAddr, &otherAddrLen)) < 0) {
			SMSC_NOTICE(1, ("recevfrom error.."));
			return;
		}
	}
	else {
		/* For TCP */
		if ((count = recv(test->sd, (char *)&stat.bytesReceived, sizeof(stat.bytesReceived), 0)) < 0) {
			SMSC_NOTICE(1, ("read error.."));
			return;
		}
		if ((count = recv(test->sd, (char *)&stat.secRecvTime, sizeof(stat.secRecvTime), 0)) < 0) {
			SMSC_NOTICE(1, ("read error.."));
			return;
		}
	}
   	SMSC_TRACE(1, ("Bytes sent=%d \nBytes received:%u Time to receive:=%d", test->byteCount, stat.bytesReceived, (int)(test->realTime)));
   	/* For throughput measurement, use the bytesReceived count from other host and use the local time. */ 
	SMSC_TRACE(1, ("Throughput: %lu Kbps", (unsigned long)((stat.bytesReceived * 8.0)/test->realTime/1000.0)));
}

int verify_data(struct tester *test, char *msgbuf, int count)
{
	int i;
	if (test->udp) 
		test->dataCount = 0;
	/*SMSC_TRACE(1,("msgbuf=%d dataCount=%d count=%d\n", msgbuf[0], test->dataCount, count));
	*/
	if(count <= 0) {
		SMSC_NOTICE(1, ("verify_data: count passed is 0"));
		return -1;
	}
	for (i = 0; i < count; i++, test->dataCount++)
	{
		if (msgbuf[i] != test->dataCount)
		{
			SMSC_NOTICE(1, ("Invalid Data, Expecting Incrementing pattern"));
			return -1;	
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -