Socket mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); mSocket.Bind(new IPEndPoint(IPAddress.Any,80)); mSocket.Listen(1000); int i = 0; while (true) { Socket http= mSocket.Accept(); System.Threading.Thread.Sleep(200); i++; Console.WriteLine("Accept:{0} ", i); }
iResult = listen(ListenSocket, 1000); if (iResult == SOCKET_ERROR) { printf("listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } int i=0; while(true) { // Accept a client socket ClientSocket = accept(ListenSocket, NULL, NULL); i++; if (ClientSocket == INVALID_SOCKET) { printf("accept failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); printf("accept: %d\n", i); }
if (bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0) { perror("bind"); return 1; } listen(server_sockfd,1000); sin_size=sizeof(struct sockaddr_in); int i=0; while(1) { i++; if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0) { cout<<"accept error"; return 1; } len=recv(client_sockfd,buf,BUFSIZ,0); cout << "Accept:" << i<<"Receive:"<<len; }
經過AB開啓500個用戶壓相應的程序,C#,WIN C++都會致使AB出現apr_socket_recv: Connection refused (111).但Linux c++這代碼是徹底正常,AB全部請求的鏈接都經過被Linux c++接入. linux
以上程序上全部Listen都是1000, 按理500個用戶同時接入不該該存在被拒絕的狀況,由於請求的鏈接數並沒達隊列溢出的狀況.但測試的結果很明確地說明的問題所在,winsocket下沒法同時接入這個量的鏈接,其實在測試過程250個用戶同時接入winsocket就存在拒絕接入的狀況,固然服務是不會有影響只是有些鏈接沒法被接入. c++
經過測試能夠確認是winsocket的限制,windows則沒有由於IIS是能夠抵抗這麼多用戶同時接入的.其實對於普通服務來講同時200個用戶接入已是一個不小的量了,由於持續這個量的處理每秒接入量能夠達到1-2W.但感受奇怪的是爲何Listen(1000)在winsocket下沒有起到做用呢?找了不少資料都沒找到具體緣由,若是有熟悉winsocket還有其餘參數設置的話但願能分享一下... windows