Hi all,
I have a requirement of multiple client/server application. I did it by using windows socket. I have to handle 1024 client at a time ..
Server Application: A socket is opened and listening on port 23. It creates a thread for each incoming connection to handle that connection independently. Once a client is connected, I have to start send/rcv continuously.
Client application: Client is sending connection request on port 23 and once connected; it will start send/rcv endlessly. I am giving 1024 client request from this app.
Problem 1: I am not able to send more than 508 connection request. I tried multiple synchronization techniques but all in vein, also not able to get the root cause of this limitation.
Problem 2: Once client/server starts communicating, my machine is getting hanged after some time because of this much thread is running by one process.
Client code:
========
I am creating 1024 threads (pretending 1 thread for each client).
Server Code:Code:// Set an event in the start of the application WaitForSingleObject(g_Flag, INFINITE); SOCKET hClientSocketHandle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); struct sockaddr_in stRemote; stRemote.sin_addr.s_addr = inet_addr("10.10.100.198"); stRemote.sin_family = AF_INET; stRemote.sin_port = htons(g_nPort); int nAddrLen = sizeof(stRemote); if(connect(hClientSocketHandle,(struct sockaddr *)&stRemote, nAddrLen) != 0 ) { closesocket(hClientSocketHandle); hClientSocketHandle = INVALID_SOCKET; SetEvent(g_Flag); return 1; } SetEvent(g_Flag); // Tell to other waiting thread to fo ahead .. int nBytesRcvd = 0; do { char szBufferByteArray[100] = {'\0'}; //Start recieving the data on the socket. This is a blocking call and will return anly when data is recieved nBytesRcvd = recv(hClientSocketHandle, szBufferByteArray, 100, 0); if(nBytesRcvd > 0) { char cSendBuffer[2] = {'\0'}; cSendBuffer[0] = '*'; int nBytesSent = send(hClientSocketHandle, cSendBuffer, 2, 0); if(nBytesRcvd == SOCKET_ERROR) { continue; } } }while(nBytesRcvd > 0);
=========
In server code, I have dedicated listener thread, which keeps listen for the connection request. Once gets one, create a dedicated thread for communication.
Listener thread:
In each client thread (once connection established):Code:while(bKeepListning) { ++g_nCurrentThreadCount; WaitForSingleObject(g_hSockethandleReceieveFlag, INFINITE); g_hNewSocket = accept(ListenSocket, (struct sockaddr *)&stRemote, &nAddrLen); if(g_hNewSocket == INVALID_SOCKET) { return 0; } //If thread exit condition is not yet met then st the connecting entity as current desktop in stGlobalHHInfo structure int nCurrentThreadCount = g_nCurrentThreadCount; HANDLE hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SocketHandleThread, (LPVOID)&nCurrentThreadCount, 0, NULL); if(hThreadHandle == NULL) { closesocket(ListenSocket); return FALSE; } }
Could anybody suggest the reason/solution for above mentioed 2 problems.Code:DWORD WINAPI SocketHandleThread(LPVOID lpParam) { int* nThreadCount = (int*) lpParam; int count = 0; int nBytesSent = 0, nBytesRcvd = 0; SOCKET hNewSocket = g_hNewSocket; SetEvent(g_hSockethandleReceieveFlag); int i = 1; while(true) // g_nTotalLoop tells how many times the file has to be sent. { nBytesSent = send(hNewSocket, g_SendData[count], g_SendDataL[count], 0); if(nBytesSent == SOCKET_ERROR) { break; } do { char buff[10] = {'\0'}; nBytesRcvd = recv(hNewSocket, buff, sizeof(buff)/sizeof(char), 0); if(nBytesRcvd > 0) { if(strcmp(buff, "*") == 0){ break;} continue; } }while(1); ++count; Sleep(2000); // wait for 2 sec before sending another message .. } }
Thank you.



LinkBack URL
About LinkBacks


