The number of threads created in the process receives a bus limit, 32 bits can access up to 4G of memory, where 2G is used by the user state, and each thread has its own stack size; test discovery uses CreateThread to create threads; When the stack is set to 1M, only about 1426 threads can be opened, when set to 512k , can open 2,244 threads, set to 256k, you can open 3,122 threads, so when we do sock communications Server, we need to be aware that if a client connect in, with a thread to its process, the server will receive a limit on the number of threads, the test code is as follows:
Server
Server.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <winsock2.h> #include <stdio.h> #include <list>using namespace std;# Define Stack_size 256*1024#define max_count 10000bool stop=false;dword WINAPI startthreadfunc (PVOID pvparam) {DWORD DwT Id 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 Wsastart Up () \ 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 which 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 Socketif (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 Socketclosesocket (listensocket); WSACleanup ();p rintf ("Server exit\n"); GetChar (); return 0;}
Client
Client.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdio.h> #include "winsock2.h" #define MAX_COUNT 10000int _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 winsockwsadata 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 serverconnectsocket[i] = socket (af_inet, Sock_st Ream, 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How many threads can be opened by the next session of Windows