In this article
Before you start
This page is one part of the distributed systems interview guide.
Why interviewers ask this
A candidate who cannot explain ports usually cannot debug a connection problem, so the question shows up early in backend interview questions and in networking warm-ups for system design. Interviewers use it to separate memorised answers from understanding. It is reported by candidates as a short opener that leads to follow-ups.
A weak answer says "a port is a number that identifies a service" and stops. A strong answer says that the IP address picks the machine, the port picks the program on it, and the number is a 16-bit field in the transport header. A Staff-level answer adds the part that explains real systems: a connection is named by four values, not one, so a server listening on a single port can talk to very many clients, and a client has a limited pool of source ports.
Port number from first principles
- Port number
- A port number is a 16-bit whole number carried in the header of a transport protocol message that tells the receiving machine which program or service should get the data, so many programs can share one IP address.
- Socket
- A socket is a network endpoint that marks one end of a conversation, written as an IP address together with a port number, for example 198.51.100.10 and port 80. Two sockets, one at each end, describe a connection.
- Ephemeral port
- An ephemeral port is a short-lived port number that a client's operating system picks for the client side of one connection, usually from the dynamic range at the top of the port space.
In plain words
Think of an apartment building. The street address is the IP address. It gets the mail to the right building. The apartment number is the port. It gets the mail to the right door.
The analogy breaks in one place. A connection has two ends, so both the sender and the receiver have an apartment number. The server's number is fixed and well known. The client's number is picked by the operating system on the client machine, usually with some randomisation (Request for Comments (RFC) 6056), without asking the user or the program.
In one precise sentence, a port number is a 16-bit label that a machine uses to hand each incoming message to the right program. The standards that define it are published as Requests for Comments (RFC).
Step-by-step description
- The destination port picks the program.
- System, user and dynamic ports.
- Different four-tuples, one server port.
- TCP 53 and UDP 53 are separate.
- The source port and destination port are each 16 bits wide.
- The machine reads the destination port and hands the data to the program that is listening on it.
- The ranges hold 1,024, 48,128 and 16,384 numbers.
- The three four-tuples are all different, so the server tells the connections apart.
- TCP port 53 and UDP port 53 are two different things.
Step 1: A port number is a 16-bit field that picks a program
TCP is a protocol that delivers a stream of bytes in order between two programs. Its header starts with two fields: a source port and a destination port, each 16 bits wide (Request for Comments (RFC) 9293, August 2022). That specification obsoletes the original RFC 793 (September 1981), which defined the same two 16-bit fields. The User Datagram Protocol (UDP) is a lighter protocol that sends separate messages with no connection, and it also labels messages with a source port and a destination port (RFC 768, August 1980).
Input: a message arriving at a machine. The machine reads the destination port and hands the data to the program that is listening on it. Output: the right program gets the data.
Sixteen bits can hold 65,536 different values (two multiplied by itself sixteen times), from 0 to 65535.2 That is the whole port space. A port is not a physical connector; it is only a number in a header. You now know what a port is; the next step shows how the 65,536 numbers are divided.
Step 2: Three ranges split the 65,536 numbers
The Internet Assigned Numbers Authority (IANA) keeps a registry of which service uses which port. RFC 6335 (August 2011) splits the space into three ranges: System Ports 0 to 1023, also called well-known ports; User Ports 1024 to 49151, also called registered ports; and Dynamic Ports 49152 to 65535, also called private or ephemeral ports. IANA assigns the first two ranges. The dynamic range is never assigned.
The ranges hold 1,024, 48,128 and 16,384 numbers.2 In the IANA registry, the Secure Shell (SSH) protocol is listed on port 22 (ssh) and the Hypertext Transfer Protocol (HTTP) on port 80 (http); the table below gives the transports each entry lists.3
| Range | Port numbers | How many | Assigned by IANA |
|---|---|---|---|
| System (well-known) | 0 to 1023 | 1,024 | Yes |
| User (registered) | 1024 to 49151 | 48,128 | Yes |
| Dynamic (ephemeral) | 49152 to 65535 | 16,384 | Never |
Learners meet a few assignments again and again, so the registry entries below are worth knowing. Port 8080 is listed as http-alt, an alternative port for the Hypertext Transfer Protocol, so a web service can run there when port 80 is taken.3
| Registry name | Port | Transports listed in the registry | Usually run over |
|---|---|---|---|
| ssh (Secure Shell) | 22 | tcp, udp, sctp | tcp |
| http (Hypertext Transfer Protocol) | 80 | tcp, udp, sctp | tcp |
| https (HTTP over Transport Layer Security (TLS), Hypertext Transfer Protocol Secure) | 443 | tcp, udp, sctp | tcp (udp for HTTP/3) |
| domain (Domain Name System) | 53 | tcp, udp | both |
| smtp (Simple Mail Transfer Protocol) | 25 | tcp, udp | tcp |
| mysql | 3306 | tcp, udp | tcp |
| postgresql | 5432 | tcp, udp | tcp |
| http-alt | 8080 | tcp, udp | tcp |
Text description of the diagram
- System ports number 1,024.
- User ports number 48,128.
- Dynamic ports number 16,384.
Step 3: The four-tuple keeps connections to one port apart
A server listens on one fixed port, yet it serves many clients. It can do so because a connection is named by four values: the client's IP address and port, and the server's IP address and port. The four-tuple is the set of these four values. The client side of each connection uses an ephemeral port that the client's operating system picks.
Take three illustrative connections to the server at 198.51.100.10 on port 80. Client A at 203.0.113.5 opens one from source port 52814 and another from 52815. Client B at 203.0.113.77 opens one from source port 52814.2 The source port 52814 appears twice, yet the three four-tuples are all different, so the server tells the connections apart. All three share one destination address and port.2
So the server's single port does not limit the number of clients. What stays constant is the destination; the source side varies. For sizing questions, the limit moves to the client: see back-of-the-envelope estimation and the follow-up below on running out of source ports.
Step 4: TCP and UDP keep separate port spaces
TCP port 53 and UDP port 53 are two different things, because the port number is read together with the protocol. The IANA registry reflects this: it lists a service name once for TCP and once for UDP, each with its own row. For example, SSH has a TCP entry and a UDP entry, both on port 22.3 RFC 6335 describes the registry as one of transport protocol port numbers.
So "port 80 is taken" is incomplete. It means port 80 is taken for one protocol on one address. A program that listens on TCP port 80 does not stop another program from using UDP port 80. This matters when you read a firewall rule or a service definition, which should always name the protocol next to the number.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →The Staff-level answer
What is a port number?
A port number is a sixteen-bit field in the TCP or UDP header. The IP address picks the machine, and the port picks the program on it. That gives a little over sixty-five thousand values, which IANA splits into system ports at the low end, user ports in the middle and dynamic ports at the top, where clients draw their source ports.
A server on one port still serves many clients, because a connection is named by four values: both addresses and both ports. The limit to watch is on the client side: one address has a limited pool of source ports for each destination, and a closed connection still holds its port for a while, so connection-heavy callers reuse connections.
What makes it Staff-level: it names the four-tuple, which explains how one port serves many clients, and it ends on the failure mode, running out of source ports, instead of stopping at the definition.
The rubric
| Dimension | Weak answer | Strong answer | Staff answer |
|---|---|---|---|
| Correctness | "A port identifies a service." | A 16-bit number that picks a program on a host | Also says it is read together with the protocol |
| Depth | Gives no range | Names the three ranges | Explains who assigns each range |
| Trade-offs | None | Notes fixed server ports and random client ports | Names source-port exhaustion on the client |
| Communication | Jargon without definitions | Uses a building analogy | Gives definition, mechanism, then limit in order |
| Follow-up readiness | Knows only the definition | Knows a port is read with the protocol | Explains how TIME_WAIT keeps client ports busy after close |
Three variants of this question
What is the difference between a port number and an IP address?
An IP address names one machine on a network, and a port number names one program on that machine. Together they form a socket, which is one end of a connection.
Can two programs listen on the same port number?
Normally not, for the same protocol and the same address: the operating system needs one program to hand each incoming connection to. Different protocols are separate, so TCP port 80 and UDP port 80 can be used by different programs. Operating systems can offer options that relax the rule, so say "normally" in an interview.
What if port numbers were 8 bits wide instead of 16?
There would be only 256 ports (two multiplied by itself eight times), against 65,536 now.2 Fixed server ports would be crowded, and clients would have a very small pool of source ports, so far fewer simultaneous connections would be possible from one address.
Common mistakes
- "A port is a physical connector on the computer." (wrong) It is a number in a packet header. (right) "A port is a 16-bit number that picks a program."
- "The port identifies the machine." (wrong) The IP address does that. (right) "The IP address picks the machine and the port picks the program on it."
- "Only ports below 1024 are officially assigned." (wrong) IANA also assigns User Ports, 1024 to 49151, and the registry has entries there (RFC 6335). (right) "IANA assigns the first two ranges; only the dynamic range is never assigned."
- "One server port means one client at a time." (wrong) The four-tuple tells connections apart. (right) "Many clients share the server port; each connection has a different four-tuple."
Follow-up questions
How many port numbers are there?
There are 65,536, numbered 0 to 65535, because the port field is 16 bits wide.2 The count is per protocol: TCP has 65,536 port numbers and UDP has a separate 65,536.
What is an ephemeral port?
An ephemeral port is the temporary port a client's operating system picks for its end of one connection. Because the operating system makes the choice, common systems use their own ephemeral ranges rather than IANA's 49152 to 65535, and the range is usually adjustable, so check the system's documentation before sizing a connection pool.
What is the difference between a well-known port and a registered port?
Well-known (System) ports are 0 to 1023 and registered (User) ports are 1024 to 49151; IANA assigns both, through different review procedures.
What happens when a client runs out of source ports?
New connections to that destination fail, because every source port is held by an open connection or by one still in TIME_WAIT after it closed (RFC 9293, section 3.3.2). TIME_WAIT is the state in which the side that closed a TCP connection first keeps its four-tuple for twice the maximum segment life (MSL), so a late packet from the old connection cannot be mistaken for a new one.
With only the IANA dynamic range, one client address holds at most 16,384 connections to one server address and port, so use connection pooling or more client addresses.2 The same limit matters for a webhook sender that calls one receiver heavily, and for a distributed rate limiter in front of an application programming interface (API) gateway.
Is a port number a security boundary?
No. A port number is a label, not a protection. Whether a service is reachable depends on firewall rules and on authentication in the service itself, which is why representational state transfer (REST) API and database servers still need access controls however their port is chosen.
- A port number is a 16-bit field that picks the program on a machine; the IP address picks the machine.
- The 65,536 ports split into System, User and Dynamic ranges, and IANA never assigns the dynamic range.
- A connection is named by four values, so one server port serves many clients.
- Port numbers are read together with the protocol; TCP and UDP are separate.
- The limit to remember is the client's pool of source ports.
Sources
- RFC 6335: IANA Procedures for the Management of the Service Name and Transport Protocol Port Number Registry — IETF / RFC Editor (BCP 165), 1 Aug 2011
- RFC 793: Transmission Control Protocol — IETF / RFC Editor, 1 Sep 1981
- IANA Service Name and Transport Protocol Port Number Registry — IANA, 26 Oct 2026
- RFC 9293: Transmission Control Protocol (TCP) — IETF / RFC Editor, 1 Aug 2022
- RFC 768: User Datagram Protocol — IETF / RFC Editor, 28 Aug 1980
- RFC 6056: Recommendations for Transport-Protocol Port Randomization — IETF / RFC Editor (BCP 156), 1 Jan 2011