eli5: why so many ports?

141 views

“TCP also introduces a concept of a port: each connection has two ports, one for both sides. A port is an integer between 0 and 65535. A server typically waits for a new connection at a well-known port. Default ports for widely-used protocols are well-established and are typically small numbers, for example, HTTP uses port 80. A client also needs its own port. These ports are typically large numbers selected automatically by an operating system, based on what ports are available.”

Why is there a need for so many ports if there is just a client and a server? If there were multiple clients I assume the port number would need to be significantly big, but 65535 is relatively small.

In: 7

6 Answers

Anonymous 0 Comments

As you say, there are often many clients, but there may also be many different servers. On my laptop, I currently have 22 different processes listening for connections on 22 different ports. And this is just my work laptop, it’s not a dedicated server.

So we do need a significant number of ports.

So why 65k specifically?

Well, we have to be able to specify the port number in the TCP packet, so we need to decide how many bits to allocated for the number. A single byte (8 bits, allowing for 256 ports) is clearly too little. So two bytes? 16 bits? That allows for 65535 different port numbers. We could go bigger, but then we’d need to make the TCP packet header bigger, and back when the TCP protocol was standardized that was a concern. It needed to be kept compact and not waste space. So 16 bits was the middle ground that seemed to provide *enough* port numbers for the kinds of uses that were foreseen at the time, without wasting more space in each TCP packet than necessary.

You are viewing 1 out of 6 answers, click here to view all answers.