客户端:
#include#include #include #include #include #include #include #include #define MAXDATASIZE 100 int connect_server(int argc, char *argv[]){ int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in their_addr; unsigned int myport; if(argv[2]) myport = atoi(argv[2]); else myport = 80; if (argc != 3) { fprintf(stderr,"usage: %s hostname port\n", argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { herror("gethostbyname"); exit(1); } if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family=PF_INET; their_addr.sin_port=htons(myport); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero),0); if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = 0; printf("Received: %s\n",buf); close(sockfd); return 0;}void* thread_process(void* none){ int argc = 3; char* argv[]={ "user","192.168.101.99","8080"}; int i = 0; while(i++<1000){ connect_server(argc,argv); }}int main(int argc, char *argv[]){ int result; int i = 0; struct timeval tpstart,tpend; float timeuse; pthread_t pthid[10]; gettimeofday(&tpstart,NULL); for(i=0;i<10;i++) pthread_create(&pthid[i],NULL,thread_process,NULL); for(i=0;i<10;i++) pthread_join(pthid[i],NULL); gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; timeuse/=1000000; printf("Used Time:%f\n",timeuse); return 0;}
服务器端:
#include#include #include #include #include #include #include #include #include /************************************* *file:socketc *data time:2013-5.1 *autho:bluesoybottle * * ***************************************/void* newfd_process(void* argv){ int new_fd = (*(int*)argv); if(send(new_fd,"HTTP 200 OK\n",14,0) == -1){ perror("send ok"); } close(new_fd);}int main_socket(int arvg_port,int listen_num){ int sockfd,new_fd; struct sockaddr_in local_addr; struct sockaddr_in user_addr; unsigned int sin_size,local_port,lisnum; if(arvg_port>0) local_port = arvg_port; else local_port = 8080; if(listen_num > 0 ) lisnum = listen_num; else lisnum = 511; if((sockfd = socket(PF_INET,SOCK_STREAM,0)) == -1){ perror("socket failed\n"); return -1; } local_addr.sin_family = PF_INET; local_addr.sin_port = htons(local_port); local_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(local_addr.sin_zero),0); if(bind(sockfd,(struct sockaddr *)&local_addr,sizeof(struct sockaddr)) == -1){ perror("bind failed\n"); return -1; } if(listen(sockfd,lisnum) == -1){ perror("listen failed\n"); return -1; } sin_size = sizeof(struct sockaddr_in); int total = 0; while(1){ if((new_fd = accept(sockfd,(struct sockaddr*)&user_addr,&sin_size)) == -1 ){ perror("accpet"); continue; } total++; printf("N:%d.server:get connection from%s\n",total,inet_ntoa(user_addr.sin_addr)); pthread_t thread_id; pthread_create(&thread_id,NULL,newfd_process,&new_fd); pthread_join(thread_id,NULL); }}int main(){ int result = main_socket(8080,-1); printf("reulst = %d\n",result); return 0;}