這篇文章主要介紹了c語言socket多線程編程,可以限制客戶端連接數,大家參考使用吧
先上一些多線程需要使用的函數定義: 代碼如下: DWORD WINAPI ProcessClientRequests(LPVOID lpParam) //新線程將會執行的函數定義 { return 0; } HANDLE handler=CreateThread(NULL, 0, ProcessClientRequests, &clientsocket, 0, NULL); //這裡比較簡單,&clientsocket是個指針,是從主線程傳入新線程的參數 WaitForMultipleObjects(MAXCLIENTS, threads, TRUE, INFINITE); //用來阻塞主線程,直到所有創建的子線程都完成任務為止,才繼續執行後面的代碼 for(int i=0;i<MAXCLIENTS; i++) { CloseHandle(threads[i]); //創建的每個子線程的HANDLE都會被保存在HANDLE數組中,這個函數用於關閉各個handle所對應的線程空間 } 服務器端程序 主線程代碼如下: 代碼如下: #define MAXCLIENTS 3 //宏定義,最多3個客戶端連接 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) //判斷是否已經超出最大連接數了 { threads[existingClientCount++]=CreateThread(NULL, 0, ProcessClientRequests, &clientsocket, 0, NULL); //啟動新線程,並且將socket傳入 } else { char* msg="Exceeded Max incoming requests, will refused this connect!rn"; send(clientsocket, msg, strlen(msg)+sizeof(char), NULL); //發送拒絕連接消息給客戶端 printf("***SYS*** REFUSED.n"); closesocket(clientsocket); //釋放資源 break; } } printf("Maximize clients occurred for d%.rn", MAXCLIENTS); WaitForMultipleObjects(MAXCLIENTS, threads, TRUE, INFINITE); //等待所有子線程,直到完成為止 closesocket(s); for(int i=0;i<MAXCLIENTS; i++) { CloseHandle(threads[i]); //清理線程資源 } WSACleanup(); printf("Cleared all.rn"); getchar(); exit(0); } 子線程函數定義 代碼如下: DWORD WINAPI ProcessClientRequests(LPVOID lpParam) { SOCKET* clientsocket=(SOCKET*)lpParam; //這裡需要強制轉換,注意:指針類型的 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; }