C Language Socket multithreaded programming limit client connections

Source: Internet
Author: User
Tags function definition int size printf socket strlen

       This article mainly introduces C language socket multithreaded programming, you can limit the number of client connections, we refer to the use of the bar

      First, some of the function definitions to use for Multithreading:     Code as follows: DWORD WINAPI Processclientrequests (LPVOID lpparam)  //new Thread The function definition that will be executed {      return 0;}   HANDLE Handler=createthread (NULL, 0, processclientrequests, &clien Tsocket, 0, NULL);  //Here is simpler, &clientsocket is a pointer to the parameters of the new thread passed from the main thread     waitformultipleobjects (maxclients, threads, TRUE, INFINITE);  //is used to block the main thread until all created child threads complete the task before continuing with the following code   for (int i=0;i<maxclients; i++) {    CloseHandle ( Threads[i]);      //created handle of each child thread will be saved in the handle array, this function is used to close the handle corresponding to the thread space}       server-side program   The main thread code is as follows:   code as follows: #define MAXCLIENTS 3          //macro definition, up to 3 client connections   int main () {  & nbsp Wsadata Wsadata;     WSAStartup (Makeword (2, 2), &wsadata);     HANDLE threads[maxclients];       SOCKET s=socket (pf_inet, Sock_stream, ipproto_tcp);       sockaddr_in sockaddr;   &NBsp Sockaddr.sin_family=pf_inet;     SOCKADDR.SIN_ADDR. S_un. S_ADDR=INET_ADDR ("127.0.0.1");     Sockaddr.sin_port=htons (9000);     Bind (s, (sockaddr*) &sockaddr, sizeof (SOCKADDR));       Listen (s, 1);       printf ("listening on port [%D].N", 9000);       int existingclientcount=0;     while (TRUE)     {        SOCKADDR clientaddr         INT Size=sizeof (SOCKADDR);           SOCKET Clientsocket;         clientsocket=accept (S, &clientaddr, &size);         printf ("***sys***    new client TOUCHED.N");           if (existingclientcount<maxclients)      //Determine if the maximum number of connections has been exceeded   &NB Sp     {          Threads[existingclientcount++]=createthread (NULL, 0, PROCESSCLI Entrequests, &clientsockeT, 0, NULL);  //start new thread, and socket incoming        }         else         {&NB Sp           char* msg= "exceeded Max incoming requests, would refused this connect!rn";             Send (Clientsocket, MSG, strlen (msg) +sizeof (char), NULL);      //Send reject connection message to client             printf ("***sys***    REFUSED.N");             closesocket (clientsocket);                                   &NB Sp Release resources             break;        }     {      printf ("Maximize clients occurred for D%.rn", maxclient S);       WaitForMultipleObjects (maxclients, threads, TRUE, INFINITE);          //Waiting for all child threads until the end of       Closesocket (s);     for (int i=0;i<maxclients; i++)     {        CloseHandle (threads[i)) &nbsp ;                                   &NB Sp    //cleanup thread resources    }       WSACleanup ();       printf ("Cleared All.rn");       GetChar ();       exit (0); }       sub-threading function definition     code is as follows: DWORD WINAPI Processclientrequests (lpvoid lpparam) {    sock et* clientsocket= (socket*) Lpparam;  //here needs to be cast, note: pointer type       char* msg= "Hello, my Client.rn";     Send (*clientsocket, MSG, strlen (msg) +sizeof (char), NULL);     printf ("***sys***    HELLO.N");       while (TRUE)     {        Char buffer[maxbyte]={0};     &NBSP ;   recv (*clientsocket, buffer, maxbyte, NULL);   &NBSp     if (strcmp (buffer, "exit") ==0         {            char* Msg_bye= "Bye.rn";             Send (*clientsocket, Msg_bye, strlen (Msg_bye) +sizeof (char), NULL);             break;        }         printf ("***client***    %SN", buffer);    }       closesocket (*clientsocket);       return 0; }    

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.