Maximum number of sockets per server?

5,763

Solution 1

Pardon me if this question doesn't make sense! Is there any theoretical limit for the number of sockets that can be used simultaneously?

No.

As per my understanding sockets are bound to ports and ports can have a value up to 65535, is this the theoretical limiting factor for the socket count?

No. For many reasons:

1) A socket can be bound to a port, but it doesn't have to be. If you just call socket, you get a socket. It's not bound to any port.

2) A socket could be associated with a protocol that doesn't use ports at all. For example, UNIX domain stream sockets don't use ports at all.

3) For TCP and UDP, each local IP address gets its own unique set of 65,536 ports.

4) TCP does not require the local IP address or port to be unique. For example, an HTTP server with IP address 1.2.3.4 and local port 80 can accept connections from a wide variety of remote IP addresses and ports. They can all have the same local IP address and port and that's not a problem because the remote IP addresses and/or remote ports are different. TCP uses all four of these things to distinguish connections.

Solution 2

You can check the link provided by @Chopper3 for a more detailed explanation, but the short answer will be no.

DETAILS

In a single IP you can have up to 65535 TCP ports + 65535 UDP ports. This is limited by the Source and Destination fields in the TCP and UDP header, which are 16 bit length. This applies both for IPv4 and IPv6.

CONSIDERATIONS

Take into account that this is a theoretical number. There are well-known ports and in a real running system there may be services that are already using some ports so there will be less ports available.

Remember that a socket is always bounded to a IP and port number. So if you need more ports you can always allocate more IPs for your host. Each IP would double your amount of available ports. You can easily achieve this:

  • If you have/need to use multiple NICs, by configuring additional IPs in each device.
  • If you are using one single NIC you can create as many virtual interfaces as you need and assing IP addresses to them.
  • Assing mutliple IPs to the same interface (issued by the ip command).
Share:
5,763

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    Pardon me if this question doesn't make sense! Is there any theoretical limit for the number of sockets that can be used simultaneously ? As per my understanding sockets are bound to ports and ports can have a value up to 65535, is this the theoretical limiting factor for the socket count?

  • David Schwartz
    David Schwartz over 5 years
    Not to mention, not every socket has to have a unique local IP address and port. An HTTP server bound to one IP address and one part can still have a huge number of different connection to it each with its own socket. If the remote IP addresses are different, that's sufficient.
  • kasperd
    kasperd over 5 years
    There will still only be one socket after a fork call. There will be references to that socket from two different file descriptor tables, so the reference count on the socket will have been increased. But it's still the same socket referenced by both. This however does mean that there is no such thing as a mapping from sockets to processes, and every tool which claims to tell you which process a socket belongs to can only give you an approximate answer.
  • kasperd
    kasperd over 5 years
    When using IP addresses in examples please follow RFC 3849 and RFC 5737.
  • kasperd
    kasperd over 5 years
    You are quoting the wrong paragraph of that page. The correct paragraph is "When all file descriptors associated with an open file description have been closed, the open file description shall be freed." The following eight paragraphs only apply when the open file descriptor is actually freed.
  • Cade Daniel
    Cade Daniel over 5 years
    You can get more than 65535 bound sockets via SO_REUSEPORT / SO_REUSEADDR.