|
作者: z_verygood [z_verygood] 论坛用户 | 登录 |
本人在编程时建立了一个流SOCK_STREAM服务器,然后客户端通过Connect(..) 连接上来,但不管怎么做,最多连接的个数总也无法超过5000,有时是2000个不到点,有时是4000个不到点,找了一些资料,但都不管用,希望各位大虾能够给予指点(是否通过Connect(..)连接有数量限制?,一百万个连接能达到吗?),注:本人不希望使用数据报SOCK_DGRAM方式。下面是本人的测试程序: // Server.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Server.h" #include "Winsock.h" #include <afxtempl.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object #define PORT 8888 static long m_nCurrentConnection=0; typedef CList<SOCKET *,SOCKET *> CSocketList; static CSocketList m_SocketList; //等待客户端连接上来。 DWORD WINAPI WaitClient(LPVOID param); //等待客户端连接上来。 DWORD WINAPI WaitClient(LPVOID param) { WSADATA wsaData; WORD version = MAKEWORD(2, 2); if (WSAStartup(version, &wsaData) != 0) return -1; m_nCurrentConnection=0; //开Socket SOCKET listen_fd = socket(AF_INET, SOCK_STREAM, 0); /* int bKeep=0; if(setsockopt(listen_fd,SOL_SOCKET,SO_KEEPALIVE,(char FAR*)&bKeep,sizeof(int))!=0) { printf("setsockopt --- Error!\n"); } */ sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(PORT); //Bind Socket if (bind(listen_fd, (sockaddr *) &addr, sizeof(addr)) < 0) { closesocket(listen_fd); return -1; } //监听 listen(listen_fd, 20); printf("Bind Ok.....\n"); //等待接收Client while(TRUE) { int addrlen = sizeof(addr); int new_fd = accept(listen_fd, (sockaddr *) &addr, &addrlen); if(new_fd < 0 ) { printf("Accept Error.....\n"); POSITION pos=m_SocketList.GetHeadPosition(); while(pos) { SOCKET *pSocket=(SOCKET *)m_SocketList.GetNext(pos); closesocket(*pSocket); delete pSocket; } m_SocketList.RemoveAll(); return -1; } //此处生成的pSocket在GTranThread函数内被Delete SOCKET *pSocket=new SOCKET; *pSocket = new_fd ; m_SocketList.AddTail(pSocket); m_nCurrentConnection++; if(m_nCurrentConnection>3972) { printf("SOCKET:<%d>\n",new_fd); } //printf(); //DWORD dwID=0; //CreateThread(NULL, 0, GTranThread, (LPVOID) pSocket, 0, &dwID); } return -10; } void main() { DWORD dwID=0; CreateThread(NULL, 0, WaitClient, NULL, 0, &dwID); while(TRUE) { Sleep(1000); printf("当前连接数<%d>\n",m_nCurrentConnection); } } // Client.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Client.h" #include "Winsock.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object int ConnectSck(SOCKET &sck,char *pIP,int nPort); int ConnectSck(SOCKET &sck,char *pIP,int nPort) { WSADATA wsaData; WORD version = MAKEWORD(2, 2); if (WSAStartup(version, &wsaData) != 0) return -1; sck=socket(AF_INET,SOCK_STREAM,0); sockaddr_in destAddr; memset(&destAddr, 0, sizeof(destAddr)); destAddr.sin_family = AF_INET; if ((destAddr.sin_addr.s_addr = inet_addr(pIP)) == INADDR_NONE) { hostent *he = gethostbyname(pIP); if (he==NULL) return -1; destAddr.sin_addr = *(in_addr *) he->h_addr; } destAddr.sin_port = htons(nPort); if(connect(sck,(LPSOCKADDR)&destAddr,sizeof(destAddr))<0) { return -2; } return 0; } void Error(); void Error() { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); // Process any inserts in lpMsgBuf. // ... // Display the string. MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); // Free the buffer. LocalFree( lpMsgBuf ); } void main() { for(int i=0;i<10000;i++) { if(i==3973) { int n=0; int g=n; } SOCKET sck=0; if(ConnectSck(sck,"192.168.8.83",8888)!=0) { Error(); //printf("Connect Error!\n"); //break; } //..Sleep(10); } printf("connect ok....\n"); getchar(); } |
地主 发表时间: 11/12 10:49 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号