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*** &NBSP;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))   ; &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*** &NBSP;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*** &NBSP;%SN", buffer); } closesocket (*clientsocket); return 0; }