首页 > 代码库 > TCP 多线程并发 echo server

TCP 多线程并发 echo server

    参考了一些开源实现:

  1 //multi_thread_server.c  2 // gcc -o multi_thread_server multi_thread_server.c -lpthread  3   4   5 #include <netinet/in.h>    // for sockaddr_in  6 #include <sys/types.h>    // for socket  7 #include <sys/socket.h>    // for socket  8 #include <stdio.h>        // for printf  9 #include <stdlib.h>        // for exit 10 #include <string.h>        // for bzero 11 #include <pthread.h> 12 #include <sys/errno.h>    // for errno 13  14 #define LENGTH_OF_LISTEN_QUEUE  512 15 #define BUFFER_SIZE 128 16  17 void * talk_to_client(void *data) 18 { 19     int new_server_socket = (int)data; 20     char buffer[BUFFER_SIZE]; 21     int length ; 22     int max_times=30; 23  24     //echo back 25     while(max_times--) { 26         bzero(buffer,BUFFER_SIZE); 27         length = recv(new_server_socket,buffer,BUFFER_SIZE,0); 28         if(length==-1||length==0) 29             break; 30         send(new_server_socket,buffer,strlen(buffer),0); 31     } 32     printf("disconnect...\n"); 33     close(new_server_socket);  34     pthread_exit(NULL); 35 } 36  37 void Usage(char *progname) { 38     fprintf(stderr,"Usage: %s -p [port_num]\n", progname);         39     fprintf(stderr,"Usage: nohup %s -p [port_num] &>%s.log &\n",progname,progname);         40 } 41  42 int main(int argc, char **argv) 43 { 44     //ÉèÖÃÒ»¸ösocketµØÖ·½á¹¹server_addr,´ú±í·þÎñÆ÷internetµØÖ·, ¶Ë¿Ú 45     struct sockaddr_in server_addr; 46     bzero(&server_addr,sizeof(server_addr)); //°ÑÒ»¶ÎÄÚ´æÇøµÄÄÚÈÝÈ«²¿ÉèÖÃΪ0 47     server_addr.sin_family = AF_INET; 48     server_addr.sin_addr.s_addr = htons(INADDR_ANY); 49  50     int port; 51     int i; 52  53     if (argc >1) { 54         for(i=1; i<argc; i++) { 55             if ((argv[i][0] == -) || (argv[i][0] == /)) { 56                 switch(tolower(argv[i][1])) { 57                      case p: 58                         port=atoi(argv[++i]);server_addr.sin_port = htons(port); 59                         break; 60                     default: 61                         Usage(argv[0]); 62                         return 1; 63                         break; 64                 } 65             } 66         } 67     } 68     else { 69         Usage(argv[0]); 70         return 1; 71     } 72  73     //´´½¨ÓÃÓÚinternetµÄÁ÷ЭÒé(TCP)socket,ÓÃserver_socket´ú±í·þÎñÆ÷socket 74     int server_socket = socket(AF_INET,SOCK_STREAM,0); 75     if( server_socket < 0) 76     { 77         printf("Create Socket Failed!"); 78         exit(1); 79     } 80      81     // set time out parameters 82     /* 83     int timeout_s=1000;//ms 84     int err=setsockopt(server_socket,SOL_SOCKET,SO_RCVTIMEO,(const char *)&timeout_s,sizeof(timeout_s)); 85     if(err!=0) 86       { 87           printf("error:setsockopt \n"); 88           close(server_socket); 89           return 1; 90       }     91     */ 92      93     //°ÑsocketºÍsocketµØÖ·½á¹¹ÁªÏµÆðÀ´ 94     if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) 95     { 96         printf("Server Bind Port : %d Failed!",port);  97         exit(1); 98     } 99     100     //server_socketÓÃÓÚ¼àÌý101     if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) )102     {103         printf("Server Listen Failed!"); 104         exit(1);105     }106     107     struct sockaddr_in client_addr;108     socklen_t length = sizeof(client_addr);109     int new_server_socket;110 111     while(1) 112     {113         new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);114         if ( new_server_socket < 0)115         {116             printf("Server Accept Failed!/n");117             break;118         }119         pthread_t child_thread;120         pthread_attr_t child_thread_attr;121         pthread_attr_init(&child_thread_attr);122         pthread_attr_setdetachstate(&child_thread_attr,PTHREAD_CREATE_DETACHED);123         if( pthread_create(&child_thread,&child_thread_attr,talk_to_client, (void *)new_server_socket) < 0 )124             printf("pthread_create Failed : %s/n",strerror(errno));125     }126 127     close(server_socket);128 129     return 0;130 }

 

TCP 多线程并发 echo server