TCP Multithreading concurrency Echo Server

Source: Internet
Author: User
Tags htons

Some open source implementations are referenced:

1 //multi_thread_server.c2 //gcc-o multi_thread_server Multi_thread_server.c-lpthread3 4 5#include <netinet/inch.h>//For sockaddr_in6#include <sys/types.h>//For socket7#include <sys/socket.h>//For socket8#include <stdio.h>//For printf9#include <stdlib.h>//For exitTen#include <string.h>//For bzero One#include <pthread.h> A#include <sys/errno.h>//For errno -  - #defineLength_of_listen_queue 512 the #defineBuffer_size 128 -  - void* Talk_to_client (void*data) - { +     intNew_server_socket = (int) data; -     CharBuffer[buffer_size]; +     intlength; A     intmax_times= -; at  -     //Echo back -      while(max_times--) { - bzero (buffer,buffer_size); -Length = recv (New_server_socket,buffer,buffer_size,0); -         if(length==-1|| length==0) in              Break; -Send (New_server_socket,buffer,strlen (buffer),0); to     } +printf"disconnect...\n"); - Close (new_server_socket); the Pthread_exit (NULL); * } $ Panax Notoginseng voidUsage (Char*progname) { -fprintf (stderr,"Usage:%s-p [port_num]\n", Progname);  thefprintf (stderr,"usage:nohup%s-p [port_num] &>%s.log &\n", Progname,progname);  + } A  the intMainintargcChar**argv) + { -     //éèöãò»¸ösocketµøö ½á¹¹server_addr,´ú±í þîñæ÷internetµøö,¶ë¿ú $     structsockaddr_in server_addr; $Bzero (&AMP;SERVER_ADDR,sizeof(SERVER_ADDR));//°ñò»¶îäú´æçøµääúèý諲¿éèöãîª0 -server_addr.sin_family =af_inet; -SERVER_ADDR.SIN_ADDR.S_ADDR =htons (inaddr_any); the  -     intPort;Wuyi     inti; the  -     if(Argc >1) { Wu          for(i=1; i<argc; i++) { -             if((argv[i][0] =='-') || (argv[i][0] =='/')) { About                 Switch(ToLower (argv[i][1])) { $                       Case 'P': -Port=atoi (Argv[++i]); server_addr.sin_port =htons (port); -                          Break; -                     default: AUsage (argv[0]); +                         return 1; the                          Break; -                 } $             } the         } the     } the     Else { theUsage (argv[0]); -         return 1; in     } the  the     //´´½¨óãóúinternetµäá÷ðòé (TCP) socket,óãserver_socket´ú±í Þîñæ÷socket About     intServer_socket = socket (Af_inet,sock_stream,0); the     if(Server_socket <0) the     { theprintf"Create Socket failed!"); +Exit1); -     } the     Bayi     //set Time Out parameters the     /* the int Timeout_s=1000;//ms - int err=setsockopt (Server_socket,sol_socket,so_rcvtimeo, (const char *) &timeout_s,sizeof (timeout_s)); - if (err!=0) the       { the printf ("error:setsockopt \ n"); the Close (server_socket); the return 1; -       }     the     */ the      the     //°ñsocketºísocketµøö ½á¹¹áªïµæðà´94     if(Bind (Server_socket, (structsockaddr*) &server_addr,sizeof(SERVER_ADDR))) the     { theprintf"Server Bind Port:%d failed!", Port);  theExit1);98     } About      -     //Server_socketóãóú¼àìý101     if(Listen (Server_socket, length_of_listen_queue))102     {103printf"Server Listen failed!"); 104Exit1); the     }106     107     structsockaddr_in client_addr;108socklen_t length =sizeof(CLIENT_ADDR);109     intNew_server_socket; the 111      while(1)  the     {113New_server_socket = Accept (Server_socket, (structsockaddr*) &client_addr,&length); the         if(New_server_socket <0) the         { theprintf"Server Accept failed!/n");117              Break;118         }119 pthread_t Child_thread; - pthread_attr_t child_thread_attr;121Pthread_attr_init (&child_thread_attr);122Pthread_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 )124printf"pthread_create Failed:%s/n", Strerror (errno)); the     }126 127 Close (server_socket); - 129     return 0; the}

TCP Multithreading concurrency Echo Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.