• C

Am I using accept correctly

I recently asked a question about an error I was getting with accept().
This was 'Too many open files'.  I am unsure if I am using accept() correctly.
I have put in a rough chunk of code below as to what I am doing.
I would be greatful for any comments, maybe you might see what my problem is.
The application is there to run constantly in a loop servicing clients.
It seems to be over a period of time I get the 'Too many open file' error
message which leads me to believe I am doing something wrong and leaving
open sockets that are just floating about.
Once the client has been accepted it passes some information to this application
it actions it and sends back to the client its results and then should close the socket.


int    highestfd,mode;
int    server_socket;
int    slot,clientsock;
struct timeval timer;
struct sockaddr addie;


// set up the unix domain socket
listen(server_socket,250);

while(1) {

  FD_ZERO(&this_set);
  FD_ZERO(&none_set);
  FD_SET(server_socket,&this_set);
  highestfd=server_socket;

  timerclear(&timer);
  timer.tv_usec = 500000;
  timer.tv_sec = 0;

  mode=select(highestfd+1,&this_set,&none_set,&none_set,&timer);

  if(mode == -1) {
    /* error */
  }
  else if(mode > 0) {
    if (FD_ISSET(server_socket,&this_set)) {
      i = sizeof(addie);
      clientsock = accept(server_socket,(struct sockaddr *)&addie,(int *)&i);
   
      if(clientsock < 0) {
        /* error */
      }
      else {
        /* client was accepted sucessfully */
        get_clients_request();
        send_back_result_to_client();
        close(clientsock);
      }
    }
  }
}
checkinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alexoCommented:
Sockets use file handles.  Make sure you don't open more than the limit (usually 255) or up the limit.
0
yshinkarCommented:
1.Use FD_CLR macro to remove server_socket from this_set when you don't need it, and reset highestfd.
2.Check if hightstfd is less than FD_SETSIZE
0
ufolk123Commented:
Hi Checkin,

if you are using   fork() in any of
get_clients_request();  or                           send_back_result_to_client();

then you need extra care in handling close of all inherited socket desc except the fd returned fron accept() call.

Regards,
ufolk123
0
aperdonCommented:
Forgot to bind the socket to a port?

Alex.
0
antoinebfCommented:
We had a similar problem with incoming connections (in a client/server app) on linux. The limit is definitly 255 file handles. What we did to solve this limitation, is to spawn external processes that deal with the incoming connections. These processes and the core communicate via sockets (each core can have up to 255 satellites... 255x255 file descriptors available).

Simply put... we have a central core for main processing, and satellites for low-level communication (in your case, it would be file-writing).

Hope this will help...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.