9

I am going to develop a TCP server application. I am on the "choose server" step. My TCP server is going to have 2000+ clients and one socket to each client.

Is there limit for amount of created sockets depending on the operating system? Which operating system permits more open sockets at a given time?

thehilmisu
  • 342
  • 1
  • 5
  • 13
  • Is 'choose server' before or after 'choose programming language'? – corsiKa Feb 25 '14 at 20:40
  • 5
    @corsiKa: The maximum number of sockets that can be open at the same time depends on the OS, not on the programming language. Unless you use a language that artificially limits the number of open file descriptors, of course. – Guntram Blohm Feb 25 '14 at 20:41
  • @GuntramBlohm Of if he chooses to handle the driver and connections on his own.. but that's insane and hard core.. Also there's limitations in the hardware :) – Torxed Feb 25 '14 at 20:44
  • @Guntram explain that to the guys in my post who have over 1 million running on a single box. *shrug* – corsiKa Feb 25 '14 at 20:44

2 Answers2

8

Yea there are limitations, but you'll probably never get close to them (connections is not the same as connecting or incomming connections, a connection is something that has happned and is established and that number is significantly higher than other states. @corsiKa gave a good quote on the number of connected sessions you can have.)

Here are some useful commands:

# Shows some general useful information,
ulimit -a

# if not, here are some other places to look
# maximum files:
cat /proc/sys/fs/file-max  

# maximum filedescriptors:
cat /proc/sys/fs/file-nr  

# maximum backlog of unaccepted clients:
cat /proc/sys/net/core/somaxconn

# And number of threads to run at once:
cat /proc/sys/kernel/threads-max

What limits how many open You->Them connections is basically how many local ports you have availible and assigned as your pool, you can find this information in:

sysctl net.ipv4.ip_local_port_range

There's also a "buffert" on incoming ports that limits how many clients you can simultaionsly have connecting to you, find this information here:

sysctl net.ipv4.tcp_max_syn_backlog
sysctl net.core.netdev_max_backlog

Also, find a complete description here: Increasing the maximum number of tcp/ip connections in linux

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 1
    If all clients connect to you, the local port range doesn't even matter. Think of an http server that can serve 1000s of connections from one single port 80. – Guntram Blohm Feb 25 '14 at 20:42
  • @GuntramBlohm I know sherlock :) But the OP wrote `is there limit for creating socket` and in my world, **creating** a socket is the same as "i will be connecting to something" and that is client-side. So i just gave enough information to cover both the client and server side parameters as they differ quite a lot :) But yea you're technically correct.. a server can host 1000's if not more on a single port.. That's where the *netdev_max_backlog* comes into play :) – Torxed Feb 25 '14 at 20:46
  • 'somaxconn' isn't the 'maximum half-open connections', it is the maximum backlog, and the backlog isn't 'the number of clients you can have simultaneously connecting to you' either, it is the length the queue of connections which have been completed by TCP but not yet passed to accept. -1 – user207421 Feb 25 '14 at 22:55
  • @EJP Can you please define the difference between "connecting to you" and "not yet accepted" then, because to me those are the same thing. Also halv-open connections is the same thing as the backlog, it's a connection waiting to happen but not yet accepted and fully open. Ergo, half open. Not sure what you're confusing this with? Or am i that way off? – Torxed Feb 26 '14 at 06:39
  • The only confusion here is yours. (1) You can fill the backlog queue with *one* client and enough *sequential* connections. *Ergo* it has nothing to do with simultaneity or multiple clients. (2) A connection on the backlog queue is complete and open in all senses: SYN, SYN/ACK, and ACK have all been exchanged, and the client can send or receive, although he will block in receive of course until the server sends something, assuming blocking-mode etc. (3) A half-open connection in TCP is one which has been *half-closed,* with `shutdown(fd,SHUT_WR).` You are that way off. – user207421 Feb 26 '14 at 07:45
7

A 2Gb Windows server should support 16,000 - so that's pretty decent, since 2Gb is rather cheap:

On Windows NT, Windows 2000, Windows XP and Windows 2003 Server, sockets are allocated from the non-paged memory pool so the actual number of sockets that can be created system-wide depends on the amount of physical memory that is installed. The non-paged memory pool is 1/8th the size of physical RAM, with a maximum of 128Mb on Windows NT and 256Mb on Windows 2000 and later platforms. The theoretical maximum for Windows NT servers is approximately 12,000 sockets, and 25,000 for Windows 2000 and later versions. In practical terms, it is safe to estimate that the Windows Server platforms can allocate approximately 4,000 sockets for every 512Mb of physical memory. For Windows NT, this means that the maximum number of sockets will be around 8,000 for a system with 1Gb or more of RAM. For Windows 2000 and later versions, the maximum number of sockets is around 16,000 for a system with 2Gb or more of RAM.

It appears free BSD can have over 1 million, and that was over 2 years ago:

Over the past few months we have been making a lot of improvements to our servers to increase the performance, uptime and scalability. Today we have tuned some knobs, shifted some traffic around and achieved 1 million established tcp sessions on a single machine (and with memory and cpu to spare!)

$ netstat -an | grep -c EST

1016313

So somewhere between 10^5 and 10^7 sockets, ish.

Community
  • 1
  • 1
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • 2
    That's simply not accurate, Torxed. There are examples in the wild of 10 million sustained connections at 1 million connections per second (with 10 second average duration). – corsiKa Feb 25 '14 at 20:55