先上一些多線程需要使用的函數定義:
DWORD WINAPI ProcessClientRequests(LPVOID lpParam) =CreateThread(NULL, , ProcessClientRequests, &clientsocket, , NULL); ( i=;i<MAXCLIENTS; i++ }
先附上C Socket初探這篇文章的url
改造開始,客戶端程序沒有任何改動,因此此處略(請看C Socket初探中代碼所示)
服務器端程序
主線程代碼如下:
MAXCLIENTS 3 , ), &===inet_addr(=htons(*)&sockaddr, , existingClientCount= size==accept(s, &clientAddr, &(existingClientCount< { threads[existingClientCount++]=CreateThread(NULL, , ProcessClientRequests, &clientsocket, } * msg=+( printf( } } printf( closesocket(s); ( i=;i<MAXCLIENTS; i++ } WSACleanup(); printf(
子線程函數定義
* clientsocket=(SOCKET*)lpParam; * msg=*clientsocket, msg, strlen(msg)+( buffer[MAXBYTE]={*(strcmp(buffer, )==* msg_bye=*clientsocket, msg_bye, strlen(msg_bye)+(*
運行效果圖: