标签:
进程里面创建线程数收到总线的限制,32位最多只能访问4G内存,其中2G为用户态使用;而每个线程都有自己的栈大小;测试发现使用createthread创建线程;当栈设置为1M时,只能开大约1426个线程;当设置为512k时,可以开2244个线程,设置为256k时,可以开3122个线程,所以在我们做sock通信服务器时,需要注意,如果一个客户端 connect进来,就用一个线程对它进程处理的话,服务端会收到线程数的限制;测试代码如下:
server
// server.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <winsock2.h> #include <stdio.h> #include <list> using namespace std; #define STACK_SIZE 256*1024 #define MAX_COUNT 10000 bool stop=false; DWORD WINAPI StartThreadFunc(PVOID pvParam) { DWORD dwTid; printf("%-3d:0x%x\n",pvParam,&dwTid); while (!stop) { Sleep(5000); // printf("sock run\n"); } return 0; } int _tmain(int argc, _TCHAR* argv[]) { // Initialize Winsock. if (argc<=2) { printf("param error\n"); return 1; } const char *ip=argv[1]; int port =atoi(argv[2]); WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) { printf("Error at WSAStartup()\n"); return 1; } //---------------------- // Create a SOCKET for listening for // incoming connection requests. SOCKET ListenSocket; ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ListenSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port for the socket that is being bound. sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(ip); service.sin_port = htons(port); if (bind( ListenSocket, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) { printf("bind() failed.\n"); closesocket(ListenSocket); WSACleanup(); return 1; } //---------------------- // Listen for incoming connection requests. // on the created socket if (listen( ListenSocket, 1 ) == SOCKET_ERROR) { printf("Error listening on socket.\n"); closesocket(ListenSocket); WSACleanup(); return 1; } //---------------------- // Create a SOCKET for accepting incoming requests. //SOCKET AcceptSocket; printf("Waiting for client to connect...\n"); int clientNum=0; HANDLE m_hCommun[MAX_COUNT]={NULL}; SOCKET sockNum[MAX_COUNT]={-1}; while (clientNum<MAX_COUNT) { // Accept the connection. sockNum[clientNum] = accept( ListenSocket, NULL, NULL ); if (sockNum[clientNum] == INVALID_SOCKET) { printf("accept failed: %d,clientNum=%d\n", WSAGetLastError(),clientNum); closesocket(ListenSocket); WSACleanup(); return 1; } else { m_hCommun[clientNum] = CreateThread(NULL, STACK_SIZE, (LPTHREAD_START_ROUTINE)StartThreadFunc, (PVOID)clientNum, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL); if (m_hCommun[clientNum]!=NULL) { printf("Client connected.clientNum=%d\n",clientNum); } clientNum=clientNum+1; } } //---------------------- stop=true; for (int i=0;i<MAX_COUNT;i++) { if (sockNum[i]!=-1) { closesocket(sockNum[i]); sockNum[i]=-1; } if (m_hCommun[i]!=NULL) { CloseHandle(m_hCommun[i]); m_hCommun[i]=NULL; } } // No longer need server socket closesocket(ListenSocket); WSACleanup(); printf("server exit\n"); getchar(); return 0; }
client
// client.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include "winsock2.h" #define MAX_COUNT 10000 int _tmain(int argc, _TCHAR* argv[]) { if (argc<=2) { printf("param error\n"); return 1; } const char *ip=argv[1]; int port =atoi(argv[2]); // Initialize Winsock WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) { printf("Error at WSAStartup()\n"); } SOCKET ConnectSocket[MAX_COUNT]={-1}; for (int i=0;i<MAX_COUNT;i++) { // Create a SOCKET for connecting to server ConnectSocket[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket[i] == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port of the server to be connected to. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr(ip); clientService.sin_port = htons(port); //---------------------- // Connect to server. if ( connect( ConnectSocket[i], (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) { printf( "Failed to connect.\n" ); WSACleanup(); return 1; } printf("============sock connect succ=%d\n",i); Sleep(100); } for (int i=0;i<MAX_COUNT;i++) { if (ConnectSocket[i]!=-1) { closesocket(ConnectSocket[i]); ConnectSocket[i]=-1; } } printf("exit to server.\n"); getchar(); WSACleanup(); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/smilestone_322/article/details/47448105