Issue
My C++ application creates 64-128 UDP sockets.
It creates sockets using this code:
int sock = socket(AF_INET, SOCK_DGRAM, 0);
assert(sock != -1, strerror(errno));
const u_int yes = 1;
int result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
printf("sock=%i result=%i errno=%i\n", sock, result, errno);
if(result != 0)
{
FATAL(strerror(errno));
}
However, at the moment it's only creating 2 sockets because setsockopt()
returns -1 on the third request:
sock=1023 result=-1 errno=0
(stderror(errno)
just says success
)
I'm puzzled because when I run ss
it doesn't look like many sockets are in use:
ss -s
Total: 238
TCP: 85 (estab 16, closed 40, orphaned 0, timewait 37)
Transport Total IP IPv6
RAW 2 1 1
UDP 24 17 7
TCP 45 30 15
INET 71 48 23
FRAG 0 0 0
My understanding is you may have 1023 sockets. So the above implies I should be able to create 64-128?
How/what is the problem here?
Solution
There is no limit on number of open sockets, but there is more general limit on number of opened file descriptors. The fact that it fails on fd=1023 suggests that this limit was truly hit, since on a typical Linux:
- file descriptors are assigned consecutive numbers starting with 0
- default limit (
ulimit -n
) is 1024 opened file descriptors
You can check the number of opened file descriptors with ls -l /proc/<pid>/fd | wc -l
. I suspect that you have many opened (regular) files.
Answered By - gudok