ChatGPT解决这个技术问题 Extra ChatGPT

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

What is the highest port number one can use?


G
Greg Hewgill

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.
R
Rohit Gupta

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


S
Smashery

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.
C
Community

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.


E
Eldakka

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.


M
Mosab Shaheen

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


B
Bardi Harborow

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


a
apaderno

It should be 65535.