Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what do SOMAXCONN mean in C socket programming?

Tags:

c

sockets

I didn't understand anything about somaxconn in socket programming in C( Linux Ubuntu).I searched through several sites, but all those couldn't help me much.

listen(sockfd,SOMAXCONN);

Does this mean to listen concurrently to maximum no. of connected sockets?

like image 283
user2655620 Avatar asked Aug 06 '13 06:08

user2655620


People also ask

What does listen () do in C?

The listen() function applies only to stream sockets. It indicates a readiness to accept client connection requests, and creates a connection request queue of length backlog to queue incoming connection requests.

What does socket () do in C?

The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created.

How do I change my net core Somaxconn?

Add net. core. somaxconn=1024 to /etc/sysctl. conf for it to become permanent (be reapplied after booting).

What is Tcp_max_syn_backlog?

tcp_max_syn_backlog - INTEGER Maximal number of remembered connection requests, which have not received an acknowledgment from connecting client. The minimal value is 128 for low memory machines, and it will increase in proportion to the memory of machine. If server suffers from overload, try increasing this number.


2 Answers

#include <sys/socket.h>

int listen (int socket, int backlog);

The backlog argument provides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue. Implementations may impose a limit on backlog and silently reduce the specified value. Normally, a larger backlog argument value shall result in a larger or equal length of the listen queue. Implementations shall support values of backlog up to SOMAXCONN, defined in <sys/socket.h>.

If listen() is called with a backlog argument value that is less than 0, the function behaves as if it had been called with a backlog argument value of 0.

A backlog argument of 0 may allow the socket to accept connections, in which case the length of the listen queue may be set to an implementation-defined minimum value.

As seen here.

like image 93
Nerius Avatar answered Oct 06 '22 09:10

Nerius


Simply put, the backlog is the maximum number of queued connections you want on a socket.. This queue is there so you can handle a connection from a client while others wait in line, the backlog specifies how long you want this line to be. if more clients attempt to connect to your server, more than the backlog, those connections will be dropped.

SOMAXCONN defines the maximum number you're allowed to pass to listen() which is 128 on my system.

You can read more about it in the man page

like image 13
iabdalkader Avatar answered Oct 06 '22 11:10

iabdalkader