Search code examples
c++socketsipcasyncsocketinter-process-communicat

How do I accept any number of connections using `accept()` during a certain time period?


For example, I don't how many clients will connect, but I'd like for any number of clients to be able to connect during a time period, say 10 seconds.

Right now I have something like this:

unsigned int currentTime = (unsigned int)time(NULL);
int session[100], sessionCount = 0;

// accept connections for 10 seconds:
while ( (unsigned int)time(NULL) - currentTime < 10 ) {
   session[sessionCount++] = accept( my_sock_desc, (struct sockaddr *)&client_sock_addr, &sock_size );
}

This will accept any number of connections, but obviously, the last iteration of the while loop will leave a call to accept() blocking the program from continuing.

How would I solve this problem?


Solution

  • check server code here

    pseudo code:

    listener thread:
           while(1)
            {
            :
            : 
            if(select(...)>0)
            {
            if(FD_SET(..)) 
                      if(accept(...)) dispatch_msg_to_processing_thread;
            }
            :
            :
            }
    
    processing thread: process message.