C language socket multi threaded programming limits the number of client connections
- 2020-04-02 01:58:15
- OfStack
First, some function definitions that multithreading needs to use:
DWORD WINAPI ProcessClientRequests(LPVOID lpParam) //The function definition that the new thread will execute
{
return 0;
}
HANDLE handler=CreateThread(NULL, 0, ProcessClientRequests, &clientsocket, 0, NULL); //Here's the easy part: the &clientsocket is a pointer that passes in the parameters of the new thread from the main thread
WaitForMultipleObjects(MAXCLIENTS, threads, TRUE, INFINITE); //Used to block the main thread until all the created child threads have completed the task before continuing to execute the following code
for(int i=0;i<MAXCLIENTS; i++)
{
CloseHandle(threads[i]); //The HANDLE for each child thread created is stored in the HANDLE array. This function closes the thread space corresponding to each HANDLE
}
Server-side program
The main thread code is as follows:
#define MAXCLIENTS 3 //Macro definition, up to 3 client connections
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
HANDLE threads[MAXCLIENTS];
SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sockaddr;
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
{
threads[existingClientCount++]=CreateThread(NULL, 0, ProcessClientRequests, &clientsocket, 0, NULL); //Start a new thread and pass the socket in
}
else
{
char* msg="Exceeded Max incoming requests, will refused this connect!rn";
send(clientsocket, msg, strlen(msg)+sizeof(char), NULL); //Send a reject connection message to the client
printf("***SYS*** REFUSED.n");
closesocket(clientsocket); //Release resources
break;
}
}
printf("Maximize clients occurred for d%.rn", MAXCLIENTS);
WaitForMultipleObjects(MAXCLIENTS, threads, TRUE, INFINITE); //Wait for all child threads to complete
closesocket(s);
for(int i=0;i<MAXCLIENTS; i++)
{
CloseHandle(threads[i]); //Clean up thread resources
}
WSACleanup();
printf("Cleared all.rn");
getchar();
exit(0);
}
Child thread function definition
DWORD WINAPI ProcessClientRequests(LPVOID lpParam)
{
SOCKET* clientsocket=(SOCKET*)lpParam; //Casting is required here. 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};
recv(*clientsocket, buffer, MAXBYTE, NULL);
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;
}
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201312/20131205102052.jpg? 2013115102113 ">