What is the largest TCP/IP network port number allowable for IPv4?
The port number is an unsigned 16-bit integer, so 65535.
«Hey, Jim, how many ports should we support?» «Just make it 16 bits for good measure. No one will ever have more than a few hundred open at once, tops.»
@JessieArr and since you seem to be taking a leaf out of bill gate’s book with his famous line about RAM, which was a statement made long ago that 640KB RAM would easily be enough in the future, and of course it is not.. Are you also trying to suggest that you think 16 bits/65536 isn’t enough? (And how would having a few hundred open ports make 65536 ports not enough?)
@barlop I was suggesting that when they first created ports, a single machine with hundreds of connections was probably considered a worst-case scenario. But today web servers, network devices, etc. can definitely bump up against port count limitations. Microsoft wrote an interesting Technet article about how to diagnose and avoid it in Windows environments: blogs.technet.microsoft.com/askds/2008/10/29/…
@JessieArr in both those cases it’s not really a 65536 issue, it’s an issue of A)programs not releasing connections, leaving them in a «WAIT» state that netstat shows combined with B)some earlier windows versions only going from 1024-5000 for dynamic ports.And even then, who knows if that ever even happened,since no program has ever bothered to report to anybody that it couldn’t get a dynamic port, neither has windows.So it’s a thoretical problem not even really caused by the 65536 number.The Web browser may be the biggest user of connections.I have 297 lines in my netstat output. Far from 65K
@JessieArr Most IP stacks use a tuple of Source IP address, Source port, Destination IP address and Destination port as a unique identifier for connections. This means that a server can have many many more active connections than there are available open ports, and the amount of ports only places a limitation (albeit a very large one) on the amount of open connections between a single source and a single destination. I don’t think anyone will ever be running servers on (or listening for connections on) more than 65536 ports at any one time.
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
ephemeral port range vary by system. I am running ubuntu linux with 3.19.0-43-generic kernel. $ cat /proc/sys/net/ipv4/ip_local_port_range results in output 32768 61000 . As to if one should or shouldn’t use a port in one’s system’s ephemeral port range, I suspect most if not all modern day network operating systems will skip over a port that is already in use.
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)
How to let kernel choose a port number in the range (1024,5000) in TCP socket programming
According to sockets, I must get a port number between 1024 and 5000, but I’m getting port numbers around 30,000.
What should I do?
2 Answers 2
Port numbers have a range of 0..65535 (although often 0 has special meaning). In the original BSD TCP implementation, only root can bind to ports 1..1023, and dynamically assigned ports were assigned from the range 1024..5000; the others were available for unprivileged static assignment. These days 1024..5000 is often not enough dynamic ports, and IANA has now officially designated the range 49152..65535 for dynamic port assignment. However even that is not enough dynamic ports for some busy servers, so the range is usually configurable (by an administrator). On modern Linux and Solaris systems (often used as servers), the default dynamic range now starts at 32768. Mac OS X and Windows Vista default to 49152..65535.
linux$ cat /proc/sys/net/ipv4/ip_local_port_range 32768 61000 solaris$ /usr/sbin/ndd /dev/tcp tcp_smallest_anon_port tcp_largest_anon_port 32768 65535 macosx$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last net.inet.ip.portrange.first: 49152 net.inet.ip.portrange.last: 65535 vista> netsh int ipv4 show dynamicport tcp Protocol tcp Dynamic Port Range --------------------------------- Start Port : 49152 Number of Ports : 16384
Feel free to post a link here about the cases where the IANA range is too small a pool. I’m curious how often this happens, seems like a weird default to have since it seems to only apply to some busy servers, not most cases.
Interestingly, netsh int ipv4 show dynamicport tcp on Win7x64 Ultimate returns. Start port: 1025 , Number of Ports : 64510 which was unexpected (at least for me)