?
Solved

Am I using accept correctly

Posted on 2000-01-28
5
Medium Priority
?
206 Views
Last Modified: 2010-04-15
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);
      }
    }
  }
}
0
Comment
Question by:checkin
5 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 2411602
Sockets use file handles.  Make sure you don't open more than the limit (usually 255) or up the limit.
0
 

Expert Comment

by:yshinkar
ID: 2476569
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
 
LVL 3

Expert Comment

by:ufolk123
ID: 2476785
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
 
LVL 1

Expert Comment

by:aperdon
ID: 2482297
Forgot to bind the socket to a port?

Alex.
0
 
LVL 2

Accepted Solution

by:
antoinebf earned 200 total points
ID: 2485723
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

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question