使用方法:
server:
./connect_tcp_server
client:
while : ;do ./connect_tcp_client server_ip 10 server_port DEBUG >> tcp_test.log;sleep 1;done&

server:
#include <stdlib.h> 
#include <stdio.h> 
#include <errno.h> 
#include <string.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <unistd.h>

#define portnumber 12345//服务器的端口号和客户端的端口号一定相等

int main(int argc, char *argv[]) 
{ 
	int sockfd,new_fd; 
	struct sockaddr_in server_addr; 
	struct sockaddr_in client_addr; 
	socklen_t sin_size;


	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) // AF_INET:IPV4;SOCK_STREAM:TCP
	{ 
		fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); 
		exit(1); 
	} 

	bzero(&server_addr,sizeof(struct sockaddr_in)); // 初始化,置0
	server_addr.sin_family=AF_INET;                 // Internet
	server_addr.sin_addr.s_addr=htonl(INADDR_ANY);  // (将本机器上的long数据转化为网络上的long数据)和任何主机通信  //INADDR_ANY 表示可以接收任意IP地址的数据,即绑定到所有的IP
	server_addr.sin_port=htons(portnumber);         // (将本机器上的short数据转化为网络上的short数据)端口号

	if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) 
	{ 
		fprintf(stderr,"Bind error:%s\n\a",strerror(errno)); 
		exit(1); 
	} 

	if(listen(sockfd,5)==-1) 
	{ 
		fprintf(stderr,"Listen error:%s\n\a",strerror(errno)); 
		exit(1); 
	} 

	int result;
	if((result = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&result,sizeof(int))) < 0)
	{
		return -2;
	}

	while(1) 
	{ 
		sin_size=sizeof(struct sockaddr_in); 
		if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1) 
		{ 
			printf("Accept error:%s\n\a",strerror(errno)); 
			exit(1); 
		} 
		printf("Server get connection\n"); // 将网络地址转换成.字符串
		fflush(stdout);

		close(new_fd); 
	} 

	close(sockfd); 
	exit(0); 
}
client:
#include <stdlib.h> 
#include <stdio.h> 
#include <errno.h> 
#include <string.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <error.h>

#define MOD_NOTICE      0
#define MOD_DEBUG       1

void timetostr(time_t *time, char *datatime)
{
	struct tm ptm = { 0 };

	localtime_r(time, &ptm);
	strftime(datatime, 20, "%Y-%m-%d %H:%M:%S", &ptm);
	return ;
}

int main(int argc, char *argv[]) 
{ 
	int sockfd; 
	struct sockaddr_in server_addr; 
	struct hostent *host; 

	int port = 12345;
	int mode = MOD_NOTICE;

	if(argc >= 4)
	{   
		port = strtol(argv[3],0,10);
		if(port == 0)
		{   
			fprintf(stderr,"port error\n"); 
			exit(-1);
		}   
	}   
	if(argc >= 5)
	{   
		if(strcmp(argv[4],"DEBUG") == 0)
			mode = MOD_DEBUG;
	}   

	struct timeval tv_start,tv_get_host,tv_connect;
	int i;
	double temp_time;
	double get_host_all_time = 0,connect_all_time = 0;
	double get_host_max_time = 0,connect_max_time = 0;
	double get_host_min_time = 99999999,connect_min_time = 99999999;

	char now_time[32];
	for(i = 0;i != strtol(argv[2],0,10);++i)
	{
		gettimeofday(&tv_start,NULL);
		if((host=gethostbyname(argv[1]))==NULL)
		{
			fprintf(stderr,"Gethostname error\n");
			exit(1);
		}
		gettimeofday(&tv_get_host,NULL);
		temp_time = (tv_get_host.tv_sec - tv_start.tv_sec) * 1000 + (tv_get_host.tv_usec - tv_start.tv_usec)/1000.0;
		if(mode == MOD_DEBUG)
			printf("DEBUG: get host time: %lfms\n",temp_time);
		if(temp_time > get_host_max_time)
			get_host_max_time = temp_time;
		if(temp_time < get_host_min_time)
			get_host_min_time = temp_time;
		get_host_all_time += temp_time;

		if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) // AF_INET:Internet;SOCK_STREAM:TCP
		{
			fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
			exit(1);
		}

		bzero(&server_addr,sizeof(server_addr)); // 初始化,置0
		server_addr.sin_family=AF_INET;          // IPV4
		server_addr.sin_port=htons(port);  // (将本机器上的short数据转化为网络上的short数据)端口号
		server_addr.sin_addr=*((struct in_addr *)host->h_addr); // IP地址

		gettimeofday(&tv_start,NULL);
		if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
		{
			fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
			exit(1);
		}
		gettimeofday(&tv_connect,NULL);
		temp_time = (tv_connect.tv_sec - tv_start.tv_sec) * 1000 + (tv_connect.tv_usec - tv_start.tv_usec)/1000.0;
		if(mode == MOD_DEBUG)
			printf("DEBUG: connect time: %lfms\n",temp_time);
		if(temp_time > connect_max_time)
			connect_max_time = temp_time;
		if(temp_time < connect_min_time)
			connect_min_time = temp_time;
		connect_all_time += temp_time;

		close(sockfd);
	}
	gettimeofday(&tv_start,NULL);
	memset(now_time,0,32);
	timetostr(&tv_start.tv_sec,now_time);
	printf("NOTICE: %s get host avg %lf max %lf min %lf\n",now_time,get_host_all_time / strtol(argv[2],0,10), get_host_max_time, get_host_min_time);
	printf("NOTICE: %s connect avg %lf max %lf min %lf\n",now_time,connect_all_time / strtol(argv[2],0,10), connect_max_time, connect_min_time);

	exit(0);
}