Not able to create 1024 socket request.

This is a discussion on Not able to create 1024 socket request. within the Networking/Device Communication forums, part of the General Programming Boards category; Hi all, I have a requirement of multiple client/server application. I did it by using windows socket. I have to ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Not able to create 1024 socket request.

    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).

    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);
    Server Code:
    =========


    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:
    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;
    	}
    }
    In each client thread (once connection established):

    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 ..
      }
    }
    Could anybody suggest the reason/solution for above mentioed 2 problems.

    Thank you.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Other than some thread synchronisation issues... i.e. nThreadCount.

    Given you probably have 1MB of stack space per thread, 1024 threads is 1GB of memory, at least.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    But my application dies even when no. of client are 100+ ...

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Do you have 100MB of memory... at least. To spare?

    Most likely you're thrashing, hence the massive slow down.

    Also:
    * sizeof(char) is ALWAYS 1
    * You're not checking the recv() return value.
    * Why send() only 1 byte a time, or recv() 10 bytes a time? That's terribly inefficient.
    * Who said the recv()'d buffer is NUL terminated? Using any str*() functions is very silly unless you NUL terminate it yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Select(); server help!
    By klipseracer in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-02-2008, 11:16 PM
  2. Urgent help needed in Socket Programming
    By lisa23 in forum C Programming
    Replies: 2
    Last Post: 09-16-2005, 12:18 AM
  3. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 08:40 PM
  4. Socket handles
    By nico in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-03-2005, 11:33 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21