2017-02问题记录总结:linux下网络收发包性能测试代码、.bin和.elf文件格式区别

 

1..bin和.elf的区别

.bin文件是一个纯粹的二进制文件,内容就是机器码,把它下载到某个地址之后,可以直接运行,例如tftp 0x12345678 file.bin,直接go 0x12345678之后,PC跳转到这个地址就能够运行。

.elf文件的内容除了运行的机器码之外,还有一个头,头部的内容为加载地址,重定位地址,符号表等等。当把file.elf文件下载到指定的地址的时候,例如tftp 0x1234567 file.elf,如果直接go 0x12345678是不能运行的,因为机器不能识别头部的内容,这时候需要一个Bootelf去启动或者运行。

一般来说,.bin要比.elf小一些,因为不包含头和一些section描述。两者之间是可以通过工具相互转换的。

 

2.linux下利用TCP-Socket网络编程进行性能测试

硬件环境:t1040d4rdb客户端,p1010rdb服务器,千兆网络

测试要求:每一包大小15k10ms收发一次,计算收发5次所消耗的时间和cpu使用率。

说明:根据tcp的规则,只要两块板子连接上了,测试也就无所谓客户端和服务器,最重要的是得到性能。周期10ms,在linux下可以用usleep-上一次执行消耗的时间,大致进行延时,误差不会太大,如果需要精确延时,可以使用应用层的定时器,或者用select来做,这个不赘述。

测试的思路如下:通过在收发的头尾打时间戳来计算时间,为了保证收发的过程不丢包,send和recv都采用循环收发的方式,保证数据量达到要求。

 

测试的时候,先起服务器,再起客户端,这里是计算了1000次的平均效率,收发包大小,和总的时间,测试的时候两块板子通过网线直连,具体测试程序如下:

 linux server:

/* 
 * created by xupeng :for net send and receive speed test
 * time				 :2017-03-09	
 */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h>
#include <pthread.h> 
#include <sys/time.h>

#define PERIOD_TIME		10    			//周期长度(ms)
#define SEND_REC_NUM	5 				//收发次数(per period)
#define SERVER_FLAG		's'				//服务器端buffer标识
#define CLIENT_FLAG		'c' 			//客户端buffer标识
#define TARGET_ADDR		"192.168.1.100"	//服务器的ip地址
#define PORT_NUMBER		5001			//端口号
#define BLEN_VALUE		65535			//buffer大小 
#define PACK_SIZE		15000			//每一包的大小  
#define SERVER_MODE			1
#define CLIENT_MODE			0


static  int 			send_socket;    //客户端发送描述符
static  int 			receive_socket; //服务器端每个客户端的描述符
static  char 			*buffer;		//buffer

/*服务器端网络初始化*/ 
int net_server_init(void)   
{ 
	struct 	sockaddr_in  serverAddr, clientAddr;


	int     len;    	
	int 	sock;      	
	int     size 	= PACK_SIZE;
	int 	port	= PORT_NUMBER;
	int 	blen	= BLEN_VALUE;



    if( (buffer = (char *)malloc(size)) == NULL)
	{
    	printf ("cannot allocate buffer of size %d\n\n", size);
        return -1;
	}

    memset(buffer, '0' ,size);
    
	/* setup BSD socket for transmitting blasts */
    bzero((char *) &serverAddr, sizeof (serverAddr));
    bzero((char *) &clientAddr, sizeof (clientAddr));

    if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
		{
			printf ("cannot open socket\n");
			free (buffer);
			return -1;
		}
    
    serverAddr.sin_family   = AF_INET;
    serverAddr.sin_port 	= htons (port);


    
    if( bind(sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
	{
	   printf ("bind OS_ERROR\n");
	   close (sock);
	   free (buffer);
	   return -1; 
	}

    if (listen(sock, 5) < 0 )
	{
	   printf ("listen failed\n");
	   close (sock);
	   free (buffer); 
	   return -1;
	}

    len = sizeof (clientAddr);

    while((receive_socket = accept(sock, (struct sockaddr *)&clientAddr, &len)) == -1)
    {
    	/*阻塞等待*/
    }

    if( setsockopt( receive_socket, SOL_SOCKET, SO_RCVBUF, (char *)&blen, sizeof(blen)) < 0 )
   	{
       printf("setsockopt SO_RCVBUF failed\n");
	   close(sock);
	   close(receive_socket);
	   free(buffer); 
	   return -1;  
   	} 
    
    if( setsockopt( receive_socket, SOL_SOCKET, SO_SNDBUF, (char *)&blen, sizeof(blen)) < 0 )
   	{
       printf("setsockopt SO_SNDBUF failed\n");  
	   close(sock);
	   close(receive_socket);
	   free(buffer); 
	   return -1;  
   	} 

    
//	for(i = 0; i < 100; i++)
//	{
//		nrecv = read (receive_socket, buffer, size); 
//			printf("The nrecv value is:%d\n",nrecv);
////			printf("The buffer0 value is:%c\n",buffer[0]);  
//		if(nrecv)
//		{ 
////				memset(buffer, 's' ,size);
//			nsent = write(receive_socket, buffer, size);  
//				printf("The nsent value is:%d\n",nsent);
//				udelay(10000); 
//		}
//	} 
    
    return 0; 
}

int net_client_init(void) 
{ 
	struct 	sockaddr_in	 sin;
	char 	*targetAddr	 = TARGET_ADDR;
	int 	port		 = PORT_NUMBER;
	int 	blen		 = BLEN_VALUE;
	int 	size 		 = PACK_SIZE;
	int 	nrecv;
	int		nsent;
	int 	i;
	
    if ( (buffer = (char *)malloc(size)) == NULL )
	{
    	printf("cannot allocate buffer of size %d\n", size);
        return -1;
	}
	
    memset(buffer, '0' ,size); 
    
    bzero((void *)&sin, sizeof(sin) );

    if ( (send_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0 )
	{
	printf ("cannot create socket\n");
	return -1;
	}

    sin.sin_addr.s_addr	= inet_addr(targetAddr);
    sin.sin_port		= htons(port);
    sin.sin_family 		= AF_INET;

    if (setsockopt(send_socket, SOL_SOCKET, SO_SNDBUF, (void *)&blen, sizeof(blen)) < 0 )
	{
    	printf("setsockopt SO_SNDBUF failed\n");
		free(buffer);
        close(send_socket);  
        return -1;
	}

    if ( connect(send_socket, (struct sockaddr *)&sin, sizeof (sin)) < 0 )
	{
    	printf("connect failed: host %s port %d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) );
    	free(buffer);
        close(send_socket);
        return -1;
	} 
   
//    nsent 	= write(send_socket, buffer, size); 
//    
//	for(i = 0; i < 500; i++)
//	{
//		nrecv = read (send_socket, buffer, size); 
////			printf("The nrecv value is:%d\n",nrecv);
////			printf("The buffer0 value is:%c\n",buffer[0]);  
//		if(nrecv)
//		{ 
////				memset(buffer, 's' ,size);
//			nsent = write(send_socket, buffer, size);  
////				printf("The nsent value is:%d\n",nsent);
//		}
//	}
    
    return 0;
}




void net_server_task(void* arg)
{ 
	unsigned int 	cyclic_time, i; 
	double 	execu,execu1; 	
	int     nsent;  
	int     nrecv;
	long long  total_size		= 0;
	unsigned int		total_num 		= 0;
	double	total_time 		= 0;
	int left_length		 	= 0;
	char *ptr;
	int 	size 			= PACK_SIZE;
	struct timeval t1,t2;
	
	struct timeval m1,m2;
	
	/* 创建单调速率定时器对象 */  
//	if ( rms_create(&period_cycle) != 0 )
//	{
//		printf("rms_create error:[%d]\n", errno);
//	}
	
//	memset(buffer, 's' ,size);
	nrecv = recv (receive_socket, buffer, size, 0);   
	printf("\n");
	printf("Starting transmit!\n");
	printf("\n");
	
	while(1)
	{ 

		gettimeofday(&t1, NULL);
		for(i = 0; i < SEND_REC_NUM; i++) 
		{ 

			ptr			= buffer; 
			left_length = size; 
			while(left_length > 0)
				{
//						printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
					nsent = send(receive_socket, ptr, left_length, 0);
					if(nsent < 0)
					{
						printf("send error!\n");
						nsent = 0;
					}
					left_length = left_length - nsent;
					ptr = ptr + nsent;
//						printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
//						printf("the lef length is :%d\n",left_length);
//						printf("the recv length is :%d\n",nsent[i]);
					total_size +=  nsent;  
				} 

			ptr			= buffer; 
			left_length = size; 
			while(left_length > 0)
			{
//					printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
				nrecv = recv(receive_socket, ptr, left_length, 0);
				if(nrecv < 0)
				{
					printf("recv error!\n");
					nrecv = 0;
				}
				left_length = left_length - nrecv;
				ptr = ptr + nrecv;
//					printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
//					printf("the lef length is :%d\n",left_length);
//					printf("the recv length is :%d\n",nrecv[i]);
				total_size +=  nrecv;  
			} 	
//			printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
//				printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
		
		} 
		gettimeofday(&t2, NULL);
		execu = 1000000*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
		execu = execu/1000;

		//		printf("recv and send nums:[%d]--time:[%8.3lf]ms\n",SEND_REC_NUM,execu); 
		total_time =total_time + execu;
		total_num++;
		if(total_num == 1000) 
		{
			m1.tv_sec = m2.tv_sec;
			m1.tv_usec = m2.tv_usec;
			gettimeofday(&m2, NULL);
			execu1 = 1000000*(m2.tv_sec - m1.tv_sec) + (m2.tv_usec - m1.tv_usec);
			execu1 = execu1/1000;
			printf("The total size is:%lld\n",total_size);
			total_size = total_size/1024/1024;
			printf("the execu1 value is:%lf\n",execu1);
			printf("the ppc speed is :%8.3lf\n",(total_size*1000/execu1)); 
			printf("The average time is:[%8.3lf]ms\n",(total_time/total_num)); 
			total_num 		= 0;
			total_time		= 0;
			total_size		= 0;
		}
	}  
}

void net_client_task(void* arg)
{
	int     nsent;  /* how many bytes sent */
	int     nrecv;  /* number of bytes received */
	int 	size = PACK_SIZE; 

//	memset(buffer, 'c' ,size); 
	nsent 	= write(send_socket, buffer, size); 
	
	for(;;) 
	{  
		
		nrecv = read (send_socket, buffer, size);  
//		printf("The buffer0 value is:%c\n",buffer[0]); 
//		printf("The nrecv value is:%d\n",nrecv);    
		if(nrecv)  
		{     
 
//			memset(buffer, 'c' ,size);
			nsent = write(send_socket, buffer, size); 
//			printf("The nsent value is:%d\n",nsent);
//			printf("The function is:%s.line is:%d\n",__FUNCTION__,__LINE__);
		}
	 
	}   
} 

int net_rms_test(unsigned int net_mode)  
{ 
	 pthread_t t_id1,t_id2;
	   
	 if(net_mode == 1)
	 {
		if( net_server_init() != 0)
		{
			printf("net server int failed!\n");
			return -1;
		}
		
		if ( pthread_create(&t_id1, NULL, (void *)net_server_task, 0) == -1)
		{
			printf("Error at pthread_create t_id -- errno[%d]!\n", errno);
			return -1;
		}
				pthread_join(t_id1,NULL); 
	 }
	 else
	 {
		if( net_client_init() != 0)
			{
				printf("net client int failed!\n");
				return -1;
			}
		if ( pthread_create(&t_id2, NULL, (void *)net_client_task, 0) == -1)
		{
			printf("Error at pthread_create t_id -- errno[%d]!\n", errno);
			return -1;
		}
				pthread_join(t_id2,NULL); 
	 }
	return 0;

}


int main(void)
{
	/* 添加用户代码 */
	printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
	if( net_rms_test(SERVER_MODE) !=0 )  
	{
		printf("Test Init error!\n ");
		return; 
	} 
	return ;
}

linux client:

/*created by xupeng for ppc1040d4rdb net speed test *
 *2017-03-22                                        */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h>
#include <pthread.h> 
#include <sys/time.h>



/*客户端还是服务器端模式*/
#define SERVER_MODE			1
#define CLIENT_MODE			0

#define PERIOD_TIME		10    			//周期长度(ms)
#define SEND_REC_NUM	5 				//收发次数(per period)
#define CURRENT_NET_NUM	1				//当前测试的网络路数 
#define MAX_NET_NUM		5				//最大测试的网络路数 

#define BLEN_VALUE		65535			//buffer大小 
#define PACK_SIZE		15000			//每一包的大小  

char *target_addr[MAX_NET_NUM]			= {"192.168.22.128","192.168.22.129","192.168.22.130","192.168.22.131","192.168.22.132"};
unsigned int port_number[MAX_NET_NUM]	= {5001,5002,5003,5004,5005}; 
static int send_socket[MAX_NET_NUM]		= {0};
static  char 			*buffer[MAX_NET_NUM];		//buffer1

static unsigned int net_num = CURRENT_NET_NUM;



int net_client_init(void) 
{ 
	struct 	sockaddr_in	 	 sin[MAX_NET_NUM];
	char 					 *targetAddr[MAX_NET_NUM];
	int 					 port[MAX_NET_NUM];
	int 	blen		 	 = BLEN_VALUE;
	int 	size 		 	 = PACK_SIZE;
	int		i				 = 0; 
 
	for(i = 0; i < net_num; i++) 
	{
		targetAddr[i]	= target_addr[i];

		port[i]			= port_number[i];
		
		if ( (buffer[i] = (char *)malloc(size)) == NULL )
		{
    		printf("cannot allocate buffer1 of size %d\n", size);
			return -1;
		}

		memset(buffer[i], i ,size);  

		bzero((void *)&sin[i], sizeof(sin[i]) );

		if ( (send_socket[i] = socket (AF_INET, SOCK_STREAM, 0)) < 0 )
		{
			printf ("cannot create socket[%d]\n",i);
			return -1;
		}

			printf("The target addr is:%s\n",targetAddr[i]);
						printf("The port num is:%d\n",port[i]);
		sin[i].sin_addr.s_addr	= inet_addr(targetAddr[i]);
		sin[i].sin_port			= htons(port[i]);
		sin[i].sin_family 		= AF_INET;

		if (setsockopt(send_socket[i], SOL_SOCKET, SO_SNDBUF, (void *)&blen, sizeof(blen)) < 0 )
		{
    		printf("setsockopt[%d] SO_SNDBUF failed\n",i);
			free(buffer[i]);
			close(send_socket[i]);  
			return -1;
		}
		
		if (setsockopt(send_socket[i], SOL_SOCKET, SO_RCVBUF, (void *)&blen, sizeof(blen)) < 0 )
		{
    		printf("setsockopt[%d] SO_SNDBUF failed\n",i);
			free(buffer[i]);
			close(send_socket[i]);  
			return -1;
		}
		

		if (connect(send_socket[i], (struct sockaddr *)&sin[i], sizeof (sin[i])) < 0 )
		{ 
    		printf("connect failed: host %s port %d\n", inet_ntoa(sin[i].sin_addr), ntohs(sin[i].sin_port) );
    		free(buffer[i]);
			close(send_socket[i]);
			return -1; 
		} 

		printf("server [%d] has connect!\n",i);

	}
//		printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
    return 0;
}


void net_client_task(void* arg)
{
	unsigned int 	i,j; 
	double 	execu,execu1; 	
	int     nsent[MAX_NET_NUM];  
	int     nrecv[MAX_NET_NUM];
	int		size = PACK_SIZE;
	long long  total_size		= 0;
	unsigned int		total_num 		= 0;
	double	total_time 		= 0;

	unsigned int sleep_time_reeduce		= 0;

	struct timeval t1,t2;
	
	struct timeval m1,m2;

	int left_length		 = 0;

	char *ptr;


//	printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
	/*先发一包同步*/
	for(i = 0; i < net_num; i++)
	{
		nsent[i] = send(send_socket[i], buffer[i], size, 0);
	}
//	printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
	printf("\n");
	printf("Starting transmit!\n");
	printf("\n");
	while(1)
	{ 
/*测试时钟是否准确*/
#if 0
	gettimeofday(&t1, NULL);
	usleep(10000); 
	gettimeofday(&t2, NULL);	
	printf("t2 sec is :%d t1 sec is :%d\n",t2.tv_sec,t1.tv_sec);
	printf("t2 usec is :%d t1 usec is :%d\n",t2.tv_usec,t1.tv_usec);
	execu = 1000000*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
	execu = execu/1000; 
	printf("gettime of day 1s time:[%10.5lf]ms\n",execu);  
#endif	
	/*粗略循环周期,10ms*/	
//		printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
		usleep(10000 - sleep_time_reeduce); 
		/*计算收发五次所需要的时间*/
		gettimeofday(&t1, NULL);
		for(i = 0; i < net_num; i++)
		{
			for(j = 0; j < SEND_REC_NUM; j++) 
			{ 
				ptr			= buffer[i]; 
				left_length = size; 
				while(left_length > 0)
				{
//					printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
					nrecv[i] = recv(send_socket[i], ptr, left_length, 0);
					if(nrecv[i] < 0)
					{
						printf("recv error!\n");
						nrecv[i] = 0;
					}
					left_length = left_length - nrecv[i];
					ptr = ptr + nrecv[i];
//					printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
//					printf("the lef length is :%d\n",left_length);
//					printf("the recv length is :%d\n",nrecv[i]);
					total_size +=  nrecv[i];  
				} 	
//				printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);

				ptr			= buffer[i]; 
				left_length = size; 
				while(left_length > 0)
					{
//						printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
						nsent[i] = send(send_socket[i], ptr, left_length, 0);
						if(nsent[i] < 0)
						{
							printf("send error!\n");
							nsent[i] = 0;
						}
						left_length = left_length - nsent[i];
						ptr = ptr + nsent[i];
//						printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
//						printf("the lef length is :%d\n",left_length);
//						printf("the recv length is :%d\n",nsent[i]);
						total_size +=  nsent[i];  
					} 

//				printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
			} 
		}
		gettimeofday(&t2, NULL);

		execu = 1000000*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
		sleep_time_reeduce = execu;
		execu = execu/1000;
//		printf("recv and send nums:[%d]--time:[%8.3lf]ms\n",SEND_REC_NUM,execu); 
		total_time =total_time + execu;
		total_num++;
		if(total_num == 1000) 
		{
			m1.tv_sec = m2.tv_sec;
			m1.tv_usec = m2.tv_usec;
			gettimeofday(&m2, NULL);
			execu1 = 1000000*(m2.tv_sec - m1.tv_sec) + (m2.tv_usec - m1.tv_usec);
			execu1 = execu1/1000;
			printf("The total size is:%lld\n",total_size);
			total_size = total_size/1024/1024;
			printf("the execu1 value is:%lf\n",execu1);
			printf("the ppc speed is :%8.3lf\n",(total_size*1000/execu1)); 
			printf("The average time is:[%8.3lf]ms\n",(total_time/total_num)); 
			total_num 		= 0;
			total_time		= 0; 
			total_size		= 0;
		}
	} 
} 






int net_rms_test(unsigned int net_mode)  
{ 
	 pthread_t t_id;
	   
	 if(net_mode == 1)
	 {
		 printf("please select client mode!\n");
	 }
	 else
	 {
		if( net_client_init() != 0)
			{
				printf("net client int failed!\n");
				return -1;
			}
				printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
		if ( pthread_create(&t_id, NULL, (void *)net_client_task, 0) == -1)
		{
			printf("Error at pthread_create t_id -- errno[%d]!\n", errno);
			return -1;
		}
		
		pthread_join(t_id,NULL); 
	 }
	return 0;

}


int main(void)
{
	/* 添加用户代码 */
	printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
	if( net_rms_test(CLIENT_MODE) !=0 ) 
	{
		printf("Test Init error!\n ");
		return; 
	} 
	return ;
}


PC端server代码,可以用板子和pc端连接进行测试,板子作为客户端:

 

#include <Windows.h>
#include <stdio.h>
#include "winsock.h"
#pragma comment(lib, "wsock32.lib")

#define PCAK_SIZE  15000

unsigned int pack_size_t	= PCAK_SIZE; 

char recvBuf[PCAK_SIZE] = ""; 

 
 

#if 1

DWORD WINAPI ThreadProc(LPVOID lpParameter)  
{
	SOCKET connectSocket = (SOCKET)lpParameter;
	int recvNum          = 0;
	int sendNum			 = 0;
	int left_length		 = 0;

	char *ptr;

	ptr			= recvBuf; 
	left_length = pack_size_t; 
	/*先收一包*/
	recv(connectSocket, recvBuf, pack_size_t, 0);
	printf("connected! starting recv and send...\n");
    while(TRUE)
    {
		ptr			= recvBuf;
		left_length = pack_size_t; 

//printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);	

		while(left_length > 0)
		{
//			printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
			sendNum = send(connectSocket, recvBuf, left_length, 0);
			if(sendNum < 0)
			{
				printf("send error!\n");
				sendNum = 0;
			}
			left_length = left_length - sendNum;
			ptr = ptr + sendNum;
//			printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
		}


//printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);


		ptr			= recvBuf;
		left_length = pack_size_t; 
		while(left_length > 0)
		{
//			printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
			recvNum = recv(connectSocket, ptr, left_length, 0);
			if(recvNum < 0)
			{
				printf("recv error!\n");
				recvNum = 0;
			}
			left_length = left_length - recvNum;
			ptr = ptr + recvNum;
//			printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
		} 
//printf("FUNC:%s--LINE:%d\n",__FUNCTION__,__LINE__);
//		printf("send num is:%d,recv num is:%d\n",sendNum,recvNum);

    }
	closesocket(connectSocket);
    return 0;  
}

int tcp_transfor()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	SOCKET listenSocket                 = 0;
	SOCKET connectSocket                = 0;
	char TCPClientRevBuf[1024*17]       = "";
	int num                             = 0;
	long sumRecv						= 0;
	SOCKADDR_IN tcpser_addr, tcpcli_addr;
	int client_len                      = 0;
	HANDLE hThread                      = 0;
	int blen							= 65535;


	memset(recvBuf,'s',pack_size_t);
	//套接字版本协商
	wVersionRequested = MAKEWORD(2,2);
	if (WSAStartup(wVersionRequested, &wsaData) != 0)
	{
		printf("version error!\n");
		return -1;
	}   

	//创建监听套接字
	if((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{  
		printf("TCP server socket create fail!\n");
		return -1;
	}   
	
	//格式化服务端地址数值
	memset(tcpser_addr.sin_zero,0,8);
	tcpser_addr.sin_family= AF_INET;
	tcpser_addr.sin_port = htons(5001);        
//	tcpser_addr.sin_port = htons(6000);  
	tcpser_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

	/*if(connect(TCPClientSocket,(struct sockaddr *)&tcpser_addr,sizeof(tcpser_addr))==-1)
	{
		printf("connect to server fail!\n");
		closesocket(TCPClientSocket);
		return -1;
	}*/

	if (setsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, (void *)&blen, sizeof(blen)) < 0 )
	{
		printf("setsockopt SO_SNDBUF failed\n");
		close(listenSocket);  
		return -1;
	}

	if (setsockopt(listenSocket, SOL_SOCKET, SO_RCVBUF, (void *)&blen, sizeof(blen)) < 0 )
	{
		printf("setsockopt SO_RCVBUF failed\n");
		close(listenSocket);  
		return -1;
	}


	if(bind(listenSocket , (struct sockaddr*)&tcpser_addr , sizeof(tcpser_addr)) < 0)  
    {  
        printf("bind error\n");  
        return -1;  
    }

	if(listen(listenSocket , 10) < 0)  
    {  
        printf("listen error");  
        return -1;  
    }

	while(1)
	{
		/*num = recv(TCPClientSocket, TCPClientRevBuf, 1024, 0);
		sumRecv += num;
		num = 0;*/
		client_len = sizeof(tcpcli_addr);

		if((connectSocket = accept(listenSocket , (struct sockaddr *)&tcpcli_addr , &client_len)) < 0 )  
        {  
            printf("accept error\n");  
            return -1;  
        }

		hThread = CreateThread(NULL,					//默认安全级别  
                            0,							//默认栈大小  
                            ThreadProc,					//线程函数   
                            (void*)connectSocket,       //函数没有参数  
                            0,							//创建后直接运行  
                            NULL); 
	}
}

#endif

#if 0
int my_send(int fd,void *buffer,int length)
 {
 int bytes_left;
 int written_bytes;
 char *ptr;
 
 ptr=buffer;
 bytes_left=length;
 while(bytes_left>0)
	 {
	         
			  written_bytes=send(fd,ptr,bytes_left,0);
			  if(written_bytes<=0)
			  {                  
					return(-1);
			  }
			  bytes_left-=written_bytes;
			  ptr+=written_bytes;     
	 }
 return(0);
 }
 
int my_recv(int fd,void *buffer,int length)
 {
 int bytes_left = 0;
 int bytes_read = 0;
 char *ptr;
   
 ptr=buffer;
 bytes_left=length;
 while(bytes_left>0)
	 {
		 bytes_read=recv(fd,ptr,bytes_read,0);
		 if(bytes_read<0)
		 {
			  return(-1);
		 }
		 else if(bytes_read==0)
			 break;
		  bytes_left-=bytes_read;
		  ptr+=bytes_read;
	 }
 return(length-bytes_left);
 }
 



DWORD WINAPI ThreadProc(LPVOID lpParameter)  
{
	SOCKET connectSocket = (SOCKET)lpParameter;
	int recvNum          = 0;
	int sendNum			 = 0;
	/*先收一包*/
	recv(connectSocket, recvBuf, pack_size_t, 0);
	printf("connected! starting recv and send...\n");
    while(TRUE)
    {

		sendNum = my_send(connectSocket, recvBuf, pack_size_t);


		if(sendNum == 0)
		{			
			recvNum = my_recv(connectSocket, recvBuf, pack_size_t); 
		}
//		printf("send num is:%d,recv num is:%d\n",sendNum,recvNum); 
    }
	closesocket(connectSocket);
    return 0;  
}

int tcp_transfor()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	SOCKET listenSocket                 = 0;
	SOCKET connectSocket                = 0;
	char TCPClientRevBuf[1024*17]       = "";
	int num                             = 0;
	long sumRecv						= 0;
	SOCKADDR_IN tcpser_addr, tcpcli_addr;
	int client_len                      = 0;
	HANDLE hThread                      = 0;
	int blen							= 65535;

	//套接字版本协商
	wVersionRequested = MAKEWORD(2,2);
	if (WSAStartup(wVersionRequested, &wsaData) != 0)
	{
		printf("version error!\n");
		return -1;
	}   

	//创建监听套接字
	if((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{  
		printf("TCP server socket create fail!\n");
		return -1;
	}   
	
	//格式化服务端地址数值
	memset(tcpser_addr.sin_zero,0,8);
	tcpser_addr.sin_family= AF_INET;
	tcpser_addr.sin_port = htons(5001);     
//	tcpser_addr.sin_port = htons(6000);  
	tcpser_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

	/*if(connect(TCPClientSocket,(struct sockaddr *)&tcpser_addr,sizeof(tcpser_addr))==-1)
	{
		printf("connect to server fail!\n");
		closesocket(TCPClientSocket);
		return -1;
	}*/

	if (setsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, (void *)&blen, sizeof(blen)) < 0 )
	{
		printf("setsockopt SO_SNDBUF failed\n");
		close(listenSocket);  
		return -1;
	}

	if (setsockopt(listenSocket, SOL_SOCKET, SO_RCVBUF, (void *)&blen, sizeof(blen)) < 0 )
	{
		printf("setsockopt SO_RCVBUF failed\n");
		close(listenSocket);  
		return -1;
	}


	if(bind(listenSocket , (struct sockaddr*)&tcpser_addr , sizeof(tcpser_addr)) < 0)  
    {  
        printf("bind error\n");  
        return -1;  
    }

	if(listen(listenSocket , 10) < 0)  
    {  
        printf("listen error");  
        return -1;  
    }

	while(1)
	{
		/*num = recv(TCPClientSocket, TCPClientRevBuf, 1024, 0);
		sumRecv += num;
		num = 0;*/
		client_len = sizeof(tcpcli_addr);

		if((connectSocket = accept(listenSocket , (struct sockaddr *)&tcpcli_addr , &client_len)) < 0 )  
        {  
            printf("accept error\n");  
            return -1;  
        }

		hThread = CreateThread(NULL,					//默认安全级别  
                            0,							//默认栈大小  
                            ThreadProc,					//线程函数   
                            (void*)connectSocket,       //函数没有参数  
                            0,							//创建后直接运行  
                            NULL); 
	}
}
#endif

int main()
{
	if(tcp_transfor() == -1)
	{
		printf("tcp run error!\n");
	}
	return 0;
}

 

 

猜你喜欢

转载自blog.csdn.net/deep_l_zh/article/details/69266188
今日推荐