views:

714

answers:

1

For server side programming I use the listen function as: int listen(int sockfd, int backlog);

I understand that the backlog should be less than or equal to the somaxconn set on the host system where I would run my server program. If I use SOMAXCONN as the backlog, it will be equivalent to hard-coding it to the value of SOMAXCONN that is usually defined in tcp.h as 128.

However, somaxconn is a tunable sysctl parameter, that can be modified by changing the value of /proc/sys/net/core/somaxconn or by using sysctl to modify net.core.somaxconn

People usually modify somaxconn to attain better system performance. I would like my program to take advantage by evaluating the system's somaxconn, at the time when the program was started.

I could open the file /proc/sys/net/core/somaxconn and read the contained value, but it seems quite an inelegant way of doing things, specially because I think that the filepath to somaxconn may vary, depending upon the distro.

Is there an API or sample code, that allows evaluating the somaxconn, in c/c++ ?

Also tell me if I am missing some crucial point, leading to flawed thinking.

I also want to port my application to Windows, so Windows programmers may also have some useful insights to share!

Thanks in advance, to all fellow hacks.

+2  A: 

Depending on your platform, sysctl() with kern.ipc.somaxconn may be what you're looking for.

However, if I understand your question correctly, your goal is to always use the maximum possible backlog size. It is my understanding that the backlog value passed to listen() will be silently limited to the system's configured limit. So your solution could be simply to call listen() with a "very large" backlog value.

Éric Malenfant
Then even the GOOGLE servers for e.g. have the same backlog value of 128 if they are running on Linux ??
codingfreak
@codingfreak: Not sure I understand what you mean here. As stated by mdk in its question, somaxconn is a tunable parameter. 128 is not a hard limit, it is just that SOMAXCONN is "normally" #defined as 128 in tcp.h. This value does not necessarily reflect the system's value.
Éric Malenfant