- Why do computers have 65,535 TCP ports?
- Where does this number come from?
- What does it represent?
3 Answers
- "Why do computers have 65535 TCP ports?"
Computers do not have ports. The concept of "ports" to which you're referring is strictly related to the TCP and UDP protocols of the TCP/IP networking stack. From a strictly technical point of view, both TCP and UDP are structured in such a way as to carry several kinds of information (Source port, Destination Port, Sequence Number, Data, etc.). As both "Source port" and "Destination port" are structured (within the TCP and UDP packet) with a 16 bit "field", here are the limits: source ports and destinations ports can assume all the values that can be represented with 16 bits (aka: from 0 to 65535);
- "Where does this number come from?"
See above: source ports and destination ports can assume values ranging from 0 to 65535 'cause they are both represented with a 16 bit value, inside respective packets
- "What does it actually represent?"
In short, both "source port" and "destination port" play a very important role, as they let a single "host" (a single "client", or a single "server") "offer" different services towards network clients, despite having a single IP address. In other words, without "ports", it would be much harder to have a single "server" offering a web-server (on TCP port 80 for plain HTTP; on TCP port 443 for HTTPS), a mail server (on TCP port 25 for inbound-SMTP; on TCP port 110 for POP3 access; on TCP port 143 for IMAP access), a DB server (on TCP/port 3306 for MySQL)) etc.
"Purist" readers may disagree with me about the above over-simplification and... they will be right: actually, IP-addresses and PORTS play a very important role that, in order to be fully understood, requires a proper understanding of the "encapsulation" concept (for sure, the most important concept among the ones presented in this very answer). Unfortunately, this usually require lots of times to be properly "mastered" and... that's why, my above over-simplification :-)
2TCP, UDP and perhaps other IP services, use ports for discreet communication between client and server processes among hosts using an IP network.
More detailed information can be found in this wiki article.
From a top-down perspective, ports represent distinct conversations between two hosts. For example, several DNS requests are made concurrently by a host to its designated server, each using a different source port to destination port 53. As the server fetches the answers, and serves the requests, it will send specific replies back to the same port, where it will be received by the distinct process that is expecting a reply.
From a bottom-up perspective, ports are a way of reserving a line of IP communication of separate processes by the operating system of a host. While there might be several processes making requests to the same port of a remote hosts, each process will use a different source port for its requests. This ensures that the replies will be received by the appropriate process.
For ported protocols, there are 32 bits reserved in the packet headers: 16 bits for the source port, and 16 bits for the destination port. For TCP and UDP, these are the first 32 bits immediately following the IP header; they begin at the 192nd bit. A 16 bit number has values between 0 and 65535.
65,536 is a very common number in computing, because it's 2 to the power of 16 (2^16). 2^8 is 256, and 65,536 is the square of 256.
In other words, a 16 bit binary number can represent 65,536 different integers. So that's probably where your 65,535 range comes from.
But wait, you think, I must be one digit out because 65,535 is 65,536 minus 1! Well, not if you start counting at zero! For example, an 8 bit image encodes 256 values, but normally these start at 0 and go up to 255. That's why you often see numbers like 255 or 65,535 in computing.
I'd guess that TCP ports were originally stored as a 16 bit number, hence their upper limit. I'm not sure if that's still the case. I hope that helps!
2