What is the largest TCP/IP network port number allowable for IPv4? - tcp

What is the highest port number one can use?

The port number is an unsigned 16-bit integer, so 65535.

The largest port number is an unsigned short 2^16-1: 65535
A registered port is one assigned by the Internet Corporation for
Assigned Names and Numbers (ICANN) to a certain use. Each registered
port is in the range 1024–49151.
Since 21 March 2001 the registry agency is ICANN; before that time it
was IANA.
Ports with numbers lower than those of the registered ports are called
well known ports; port with numbers greater than those of the
registered ports are called dynamic and/or private ports.
Wikipedia: Registered Ports

As I understand it, you should only use up to 49151, as from 49152 up to 65535 are reserved for Ephemeral ports

Just a followup to smashery's answer. The ephemeral port range (on Linux at least, and I suspect other Unices as well) is not a fixed. This can be controlled by writing to
/proc/sys/net/ipv4/ip_local_port_range
The only restriction (as far as IANA is concerned) is that ports below 1024 are designated to be well-known ports. Ports above that are free for use.
Often you'll find that ports below 1024 are restricted to superuser access, I believe for this very reason.

According to RFC 793, the port is a 16 bit unsigned int.
This means the range is 0 - 65535.
However, within that range, ports 0 - 1023 are generally reserved for specific purposes. I say generally because, apart from port 0, there is usually no enforcement of the 0-1023 reservation. TCP/UDP implementations usually don't enforce reservations apart from 0. You can, if you want to, run up a web server's TLS port on port 80, or 25, or 65535 instead of the standard 443. Likewise, even tho it is the standard that SMTP servers listen on port 25, you can run it on 80, 443, or others.
Most implementations reserve 0 for a specific purpose - random port assignment. So in most implementations, saying "listen on port 0" actually means "I don't care what port I use, just give me some random unassigned port to listen on".
So any limitation on using a port in the 0-65535 range, including 0, ephemeral reservation range etc, is implementation (i.e. OS/driver) specific, however all, including 0, are valid ports in the RFC 793.

Valid numbers for ports are: 0 to 2^16-1 = 0 to 65535
That is because a port number is 16 bit length.
However ports are divided into:
Well-known ports: 0 to 1023 (used for system services e.g. HTTP, FTP, SSH, DHCP ...)
Registered/user ports: 1024 to 49151 (you can use it for your server, but be careful some famous applications: like Microsoft SQL Server database management system (MSSQL) server or Apache Derby Network Server are already taking from this range i.e. it is not recommended to assign the port of MSSQL to your server otherwise if MSSQL is running then your server most probably will not run because of port conflict )
Dynamic/private ports: 49152 to 65535. (not used for the servers rather the clients e.g. in NATing service)
In programming you can use any numbers 0 to 65535 for your server, however you should stick to the ranges mentioned above, otherwise some system services or some applications will not run because of port conflict.
Check the list of most ports here: https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

It depends on which range you're talking about, but the dynamic range goes up to 65535 or 2^16-1 (16 bits).
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

It should be 65535.

Related

What is the maximum number of active connections a client can have to a server IP? (networking)

I came to my knowledge that if a machine makes 2 requests to the same destination IP and the same destination port, the source ports have to be different. But if that is the case, there must be a maximum number of active connections a client can have to a server. Is there a limit on how many such connections there can be?
A port is 16 bits, so the absolute limit would be 216.
Of course, port 0 is not really ever used and ports 1 to 1023 are clearly reserved for servers. Plus, in most cases, you have a limited range of ports you can use to connect as a client. These are called ephemeral ports and are between 49152 and 65535.
The number 49152 is 0xC000. So you get the top quarter of the ports available to clients or 16384 ports (214). That's your limit as a client.
Note that memory is also required. More or less depending on your application but also the kernel needs enough memory to allocate so many ports. So you are more likely to run out of memory before you can allocate that many ports (unless, like me, you have a computer with 512Mb of RAM or more... then you'll have a hard time to stress the memory allocation in most cases).
In practical use, I've never run out of client ports. The main issue I run into is multiple applications trying to listen on the same port (i.e. two servers trying to listen on port 80, for example).

how to make a difference between server port number and client port number?

I would like to know what is the interval ports number for servers and for clients. I have a list of port numbers and I need to separate clients and servers.
You cannot separate separate servers from clients solely based on the port number.
For example,
my_addr1.sin_family = AF_INET;
my_addr1.sin_addr.s_addr = INADDR_ANY;
my_addr1.sin_port = htons(12010);
my_addr1.sin_addr.s_addr = inet_addr("10.11.12.13");
if (bind(client, (struct sockaddr*) &my_addr1, sizeof(struct sockaddr_in)) == 0)
printf("Binded Correctly\n");
else
printf("Unable to bind\n");
will bind your client explicitly to port 12010 (see https://www.geeksforgeeks.org/explicitly-assigning-port-number-client-socket/ for a complete demo). The same goes for servers: you can easily have your server listen to some specific TCP (or UDP) port.
Luckily, there are a number of well known ports, kept by IANA (Internet Assigned Number Authority) in the Service Name and Transport Protocol Port Number Registry. Any well-thinking organization should (https://www.ietf.org/rfc/rfc2119.txt) respect these assignments. But there is no guarantee, and 'client' and 'server' may be different from your expectation (see f.e. 123: NTP).
Likewise, clients that behave nicely, in general use ephemeral port numbers. These are high port numbers which are handed out by the OS when a program asks for an available port. To complicate things: ephemeral port differ per OS:
OS from to comment
AIX 32768 65535
BSD 49152 65535
Slackware 32768 60999 cat /proc/sys/net/ipv4/ip_local_port_range
Windows 5000 65534 may differ per version.
To make things worse, there is portmapper, which, more or less dynamically, assigns port numbers to servers (in general 100001 and up). But outside NFS it is rarely used.
So, you can make a decent assumption, based on IANA's well known port numbers and the ephemeral port ranges from your OS-s, but there is no guarantee that it is correct.
On Linux, use sudo netstat -lpe to see a list of servers. On other OSes see the man page or documentation for netstat for the appropriate flags.

Port number concepts?

I am trying to understand the concept of port number. As much as I know it identifies a specific process or a network service.
Can anyone give me a real life example. So, it could be easier to understand.
Some doubts that I currently have-
I heard, there are 65536 ports. Does, that mean, a system can identify 65536 processes simultaneously?
I have seen that some ports are reserve for some specific service. So, does it mean, it can't be used for any other service?
What is the command to know which port numbers are free or to use?
What is the command to know which port numbers are not free and what are they used for?
If I try to access a system through remote desktop from my computer, it asks for a port number. What should I mention?
If possible please share a link. I am currently getting confused with too much technical theory. Thank you!!!
I heard, there are 65536 ports.
You heard wrong. There are 65535: 1 .. 65535. Zero is not a valid port number.
Does, that mean, a system can identify 65536 processes simultaneously?
It means a system can identify 65535 ports simultaneously.
I have seen that some ports are reserve for some specific service. So, does it mean, it can't be used for any other service?
That is the meaning of the word 'reserve'.
What is the command to know which port numbers are free or to use?
It isn't a command. It is either a search at the IETF website for reserved ports or the use of the number zero, which means the next available port.
What is the command to know which port numbers are not free and what are they used for?
It isn't necessarily a command. It is a search at the IETF website for reserved ports, or the netstat command for ports actively in use on the localhost.
If I try to access a system through remote desktop from my computer, it asks for a port number. What should I mention?
The port number that you're trying to connect to in the remote system.
Here is more clarification:
I heard, there are 65536 ports.
A port is 16 bit i.e. 2^16 =65536 so right
"Port Zero does not officially exist. It is defined as an invalid port number. But valid Internet packets can be formed and sent over the wire to and from port 0 just as with any other ports." https://www.grc.com/port_0.htm
Does, that mean, a system can identify 65536 processes simultaneously?
You do not care about a system, you care about an IP. For every Ip you can use 65536 processes simultaneously.
I have seen that some ports are reserve for some specific service. So, does it mean, it can't be used for any other service?
Of course, otherwise there will be a port conflict.
What is the command to know which port numbers are free or to use?
Refer my post here https://stackoverflow.com/a/54760498/2197108
Netstat: in Linux and Windows displays connections and ports
What is the command to know which port numbers are not free and what are they used for?
It should be clear now
If I try to access a system through remote desktop from my computer, it asks for a port number. What should I mention?
Remote desktop asks for IP address of the remote machine. However, it may ask for user credentials of the remote machine.
In case it asks for IP and Port it means IP and Port of the remote desktop software (server) in the remote machine.

there is a limitation of client socket connection

I made a tcp/ip echo server by using kqueue on a unix(OSX) machine.It desinged to open 100 tcp/ip ports and accept 4000 clients per port.I made about 230,000 connections by using 15 another machines. ( 4000 conn per single process, and 4 process per box)
It seems that the server could accept more than that but the problem was the client side.There is a limitation to make much connections. Such as Windows XP - 3000, Windows7 & Unix - 16384 ..
Is the limitation right ? did I wrong ? I want to make as much connections as I can on a single client, such as 500,000 / Machine.
How could I overcome ?
You can configure multiple IPv4 address on the client machine. Process that runs on client should receive source IP address as parameter, and bind connect socket to it. So with 10 IP addresses you can easily reach 400000 connections.
I want to make as much connections as I can on a single client, such as 500,000 / Machine.
You can't. A TCP connection is identified by the tuple {protocol, source address, source port, target address, target port}. Four of these five elements are fixed. The remaining element, source port, can only have 65535 distinct values at the most, as a port number has 16 bits and cannot be zero.
So your expectation of 500,000 connnections to the same client is over-optimistic by a factor of about 10.

What is the theoretical maximum number of open TCP connections that a modern Linux box can have

Assuming infinite performance from hardware, can a Linux box support >65536 open TCP connections?
I understand that the number of ephemeral ports (<65536) limits the number of connections from one local IP to one port on one remote IP.
The tuple (local ip, local port, remote ip, remote port) is what uniquely defines a TCP connection; does this imply that more than 65K connections can be supported if more than one of these parameters are free. e.g. connections to a single port number on multiple remote hosts from multiple local IPs.
Is there another 16 bit limit in the system? Number of file descriptors perhaps?
A single listening port can accept more than one connection simultaneously.
There is a '64K' limit that is often cited, but that is per client per server port, and needs clarifying.
Each TCP/IP packet has basically four fields for addressing. These are:
source_ip source_port destination_ip destination_port
<----- client ------> <--------- server ------------>
Inside the TCP stack, these four fields are used as a compound key to match up packets to connections (e.g. file descriptors).
If a client has many connections to the same port on the same destination, then three of those fields will be the same - only source_port varies to differentiate the different connections. Ports are 16-bit numbers, therefore the maximum number of connections any given client can have to any given host port is 64K.
However, multiple clients can each have up to 64K connections to some server's port, and if the server has multiple ports or either is multi-homed then you can multiply that further.
So the real limit is file descriptors. Each individual socket connection is given a file descriptor, so the limit is really the number of file descriptors that the system has been configured to allow and resources to handle. The maximum limit is typically up over 300K, but is configurable e.g. with sysctl.
The realistic limits being boasted about for normal boxes are around 80K for example single threaded Jabber messaging servers.
If you are thinking of running a server and trying to decide how many connections can be served from one machine, you may want to read about the C10k problem and the potential problems involved in serving lots of clients simultaneously.
If you used a raw socket (SOCK_RAW) and re-implemented TCP in userland, I think the answer is limited in this case only by the number of (local address, source port, destination address, destination port) tuples (~2^64 per local address).
It would of course take a lot of memory to keep the state of all those connections, and I think you would have to set up some iptables rules to keep the kernel TCP stack from getting upset &/or responding on your behalf.

Resources