c语言socket多线程编程限制客户端连接数
c语言socket多线程编程限制客户端连接数
发布时间:2017-01-07 来源:查字典编辑
摘要:先上一些多线程需要使用的函数定义:复制代码代码如下:DWORDWINAPIProcessClientRequests(LPVOIDlpPar...

先上一些多线程需要使用的函数定义:

复制代码 代码如下:

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;

}

c语言socket多线程编程限制客户端连接数1

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C语言学习
热门C语言学习
编程开发子分类