首页 > 代码库 > 一个多线程的Socket通信Demo(C语言实现)
一个多线程的Socket通信Demo(C语言实现)
-----------这是客户端程序-------------- #include "stdio.h" #include "winsock2.h" #include "stdlib.h" #pragma comment(lib,"ws2_32") void main() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); 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); connect(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR)); char buffer[MAXBYTE] = {0}; recv(s,buffer,MAXBYTE,NULL); printf(" ***SERVER*** %s",buffer); while(TRUE) { char* mymsg = new char[10000]; printf(" OK Now You Can Connect .\n ->"); gets(mymsg); send(s,mymsg,strlen(mymsg)+sizeof(char),NULL); } closesocket(s); WSACleanup(); getchar(); exit(0); }
-----这是服务端程序------
#include "stdio.h" #include "winsock2.h" #include "stdlib.h" #define MAXCLIENTS 2 #pragma comment(lib,"ws2_32") /* 多线程 SOCKET */ DWORD WINAPI ProcessClientRequest(LPVOID lpParam) { SOCKET* clientsocket = (SOCKET*)lpParam; char* msg = "Hello, Welcome to connect . \r\n"; 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* exit_msg = "Bye \r\n"; send(*clientsocket,exit_msg,strlen(exit_msg)+sizeof(char),NULL); break; } printf("--- Sys: %s--\n",buffer); } closesocket(*clientsocket); return 0; } int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); 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); char* msg = new char[1000]; HANDLE threads[MAXCLIENTS]; //线程存放 数组 int CountClient = 0; while(TRUE) { SOCKADDR clientAddr; int size = sizeof(SOCKADDR); SOCKET clientsocket; clientsocket = accept(s,&clientAddr,&size); //阻塞模式 直到有新的Tcp 接入 printf("Sys: New client touched ID is %d .\n",CountClient+1); // if(CountClient < MAXCLIENTS) //创建新线程 { threads[CountClient ++] = CreateThread(NULL,0,&ProcessClientRequest,&clientsocket,0,NULL); } else //线程数超了 拒绝服务 { char *msg = " Error Too many client Connecttion !.\r\n"; send(clientsocket,msg,strlen(msg)+sizeof(char),NULL); printf(" ** SYS ** REFUSED !.\n"); closesocket(clientsocket); } } printf("Maximize clients occurred for d%.\r\n", MAXCLIENTS); WaitForMultipleObjects(MAXCLIENTS,threads,TRUE,INFINITE); closesocket(s); //关闭socket for(int i=0;i<MAXCLIENTS;i++) { CloseHandle(threads[i]); } WSACleanup(); getchar(); exit(0); return 0; }
一个多线程的Socket通信Demo(C语言实现)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。