Ports, Names and Applications: Client/Server, Peer-to-Peer and the Sockets API
How port numbers pick the right program, the well-known, registered and ephemeral ranges, how names become addresses, internets versus intranets, client/server and peer-to-peer designs, iterative versus concurrent servers, and the sockets API, with real Linux output.
kvi5hnuCode Sprout team member20 min readOn this page
An IP (Internet Protocol) address gets a packet to the right computer. But a computer runs many networked programs at once: a browser with dozens of tabs, a mail client, a chat app, a background updater. Port numbers get each packet to the right program. Names such as www.example.com let people find computers without memorizing addresses. And a few standard application designs decide who connects to whom.
This article covers all three, and finishes with the sockets API, the programming interface nearly every networked program uses.
Port numbers
A port number is a 16-bit unsigned integer, from 0 to 65535. It's not a physical thing: it's just a number in the TCP (Transmission Control Protocol) or UDP (User Datagram Protocol) header (article 3 showed it at byte offsets 22–25 of a real packet). Each transport protocol has its own set of ports, so TCP port 53 and UDP port 53 are different, although DNS (the Domain Name System, covered below) happens to use both.
A server binds to a port to claim it, and clients connect to that port. It works a bit like a phone extension: the IP address is the company's number, and the port is the extension of the department you want.
The three ranges
The Internet Assigned Numbers Authority (IANA) keeps the official registry of port numbers and divides it into three ranges, defined in RFC 6335. (An RFC, or Request for Comments, is one of the Internet's numbered standards documents. Article 5 explains them.)

- System ports (0–1023), often called well-known ports, are assigned to standard services: SSH 22 (Secure Shell, remote login), SMTP 25 (mail delivery), DNS 53, HTTP 80 (the web), IMAP 143 (reading a mailbox), HTTPS 443 (HTTP encrypted with TLS), IMAPS 993 (IMAP with TLS), and so on. Assignments need review by the IETF (Internet Engineering Task Force, the body that writes the standards).
- User ports (1024–49151), also called registered ports, are assigned by IANA to applications that ask, such as MySQL on 3306 and PostgreSQL on 5432. Anyone can bind them. If you design a new protocol, register a port rather than picking one and hoping.
- Dynamic or private ports (49152–65535) are never assigned. They're free for temporary use.
A service that runs both with and without TLS (Transport Layer Security, the standard encryption layer) usually has two ports: HTTP 80 and HTTPS 443, IMAP 143 and IMAPS 993, SMTP submission 587 and its TLS-from-the-start version 465. HTTP/3 runs over QUIC, an encrypted transport built on UDP, on UDP port 443, which shows that the same number on TCP and UDP can mean related but different services.
What's listening
On Linux, ss lists sockets. The options -t and -u select TCP and UDP, -l shows only listening sockets, -n shows numbers instead of names, and -p shows the owning process. On the lab's server host, running nginx, the dnsmasq DNS server and a small test program on port 8443:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
udp UNCONN 0 0 198.51.100.10:53 0.0.0.0:* users:(("dnsmasq",...))
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",...))
tcp LISTEN 0 32 198.51.100.10:53 0.0.0.0:* users:(("dnsmasq",...))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",...))
tcp LISTEN 0 128 *:8443 *:* users:(("python3",...))
How to read it:
198.51.100.10:53means dnsmasq is bound to one specific address, so it only accepts packets sent to that address.0.0.0.0:80means nginx accepts IPv4 connections to port 80 on any of the host's addresses.- nginx has a second socket,
[::]:80, for IPv6. In the Peer column,0.0.0.0:*and[::]:*mean "any remote address and port": a listening socket isn't tied to one client yet. *:8443is an IPv6 socket that also accepts IPv4 (a dual-stack socket). IPv4 clients show up on it with addresses like::ffff:192.0.2.10.- DNS uses UDP for most queries, so dnsmasq has a UDP socket, which shows
UNCONN(UDP has no connections), as well as a TCP one for large answers. - For a
LISTENsocket,Recv-Qcounts connections waiting right now to be accepted (0 here), andSend-Qshows the maximum length of the queue of connections waiting to be accepted. Article 63 explains these queues.
Ephemeral ports
The client side of a connection needs a port too, but nobody needs to find it, so the operating system picks one for the life of the connection. These are ephemeral ports. Each operating system uses its own range, and the lab's Linux host shows its defaults:
net.ipv4.ip_local_port_range = 32768 60999
net.ipv4.ip_unprivileged_port_start = 1024
The first line says Linux picks client ports from 32768 to 60999. The second is the boundary for privileged ports, covered below. Here are six connections from one client, followed by three sockets that asked for "any port" by binding to port 0:
connect() got local port 40420
connect() got local port 40428
connect() got local port 40438
connect() got local port 40440
connect() got local port 40444
connect() got local port 40448
bind() to port 0 got port 58133
bind() to port 0 got port 49077
bind() to port 0 got port 40681
Two details are typical of current Linux kernels:
- Ports for
connect()are spread around the range but come out even, whilebind()to port 0 returns odd ports. Since the mid-2010s (Linux 4.2 forconnect(), 4.6 forbind()), the kernel tries even ports first for outgoing connections and odd ports first forbind(), so that busy clients don't use up the ports that servers bind. It's a preference, not a guarantee: when one half runs out, the other is used. - The ports aren't simply 1, 2, 3 higher each time. Modern systems choose ports in a hard-to-predict way (RFC 6056), which makes it harder for an attacker off the path to guess a connection's port and inject fake packets.
One port, many connections
A common misconception is that a server "uses up a port per client", or that a client can open at most about 28,000 connections because that's the size of the ephemeral range. In fact, a TCP connection is identified by four values: source address, source port, destination address and destination port (plus the protocol, which makes it the 5-tuple). So:
- A web server's single listening port 443 can carry hundreds of thousands of connections. Each has a different client address or port.
- A client can reuse the same ephemeral port for connections to different servers, or to different ports on the same server. The limit of about 28,000 applies only to simultaneous connections from one source address to one destination address and port. Busy proxies hit it, and spread connections over several source addresses to avoid it.
Privileged ports
On Unix systems, binding a port below 1024 traditionally needs root privileges. The idea was that a remote user could trust that port 22 or 25 was run by the system administrator, not by an ordinary user. In a fresh Linux network namespace, an ordinary user (here uid 65534, nobody) can't bind port 80 but can bind 8080:
running as uid 65534
bind to port 80: Permission denied (errno 13)
bind to port 8080: OK
The rule has weakened a lot:
- On Linux, the boundary is the setting
net.ipv4.ip_unprivileged_port_start, shown as 1024 above. Container runtimes such as Docker set it to 0 inside containers, which is why a container can run a web server on port 80 without root. A program can also be given just theCAP_NET_BIND_SERVICEcapability instead of full root. - macOS has let ordinary users bind low ports on the wildcard address since macOS 10.14 Mojave, and Windows has never had privileged ports.
Today it gives no real security: anyone can run their own machine as root. Security comes from authentication, such as SSH host keys and TLS certificates, not from port numbers.
Names and addresses
IP addresses identify hosts, but people want names. An IPv6 address such as 2001:db8:2::10 is hard to remember, and a server's address may change. The Domain Name System (DNS) is a distributed database that maps names to addresses and back. Names form a hierarchy that ends in top-level domains such as .com, .org, .uk or .in.
Surprisingly, DNS is an application-layer protocol, carried over UDP and TCP like any other application. IP, TCP and UDP don't use names at all. Only applications do. But since people use names for almost everything, a broken DNS feels like a broken Internet. Part 9 (articles 49 to 56) covers DNS in depth.
Programs don't usually speak DNS directly. They call the system library function getaddrinfo(), which consults the local hosts file, DNS and any other configured sources, and returns every address for the name. The lab's client resolves www.example.com, which the lab defines with one IPv6 and one IPv4 address. Each result line shows the address family (AF_INET6 is IPv6, AF_INET is IPv4), the socket type (SOCK_STREAM means TCP) and the address and port to connect to. The two extra zeros in the IPv6 result are IPv6-only fields (flow label and scope ID) that are normally 0:
AF_INET6 SOCK_STREAM ('2001:db8:2::10', 443, 0, 0)
AF_INET SOCK_STREAM ('198.51.100.10', 443)
getnameinfo: ('www.example.com', 'https')
Two things to notice:
- The IPv6 address comes first. The default address selection rules (RFC 6724) prefer IPv6 when the host has working IPv6. A well-written client tries the addresses in order, and modern ones use Happy Eyeballs (RFC 8305) to race IPv6 and IPv4 so a broken IPv6 path doesn't cause a long delay.
- The reverse function,
getnameinfo(), turns an address back into a name, and a port number back into a service name using the system's services list.
Most programs that accept a name also accept an address directly. In a URL (a web address), an IPv4 address is written as is, as in http://192.0.2.10/, and an IPv6 address goes in square brackets so its colons aren't mistaken for a port separator, as in http://[2001:db8:2::10]:8080/.
Internets, intranets and extranets
Recall from article 1 that the Internet grew by connecting networks with routers. Some terms describe the result:
- An internet (lowercase) is any set of networks connected by routers and using one protocol suite.
- The Internet (capitalized) is the single global internet. It's an internet, but most internets aren't the Internet.
- An intranet is a private internet run by one organization for its own users. People reach it remotely through a virtual private network (VPN), which tunnels their traffic in encrypted form across the Internet.
- An extranet is the part of an organization's network opened to partners, usually outside its firewall and often reached by VPN.
Technically these all use the same protocols. The differences are about who is allowed in and who runs them. Interconnection is worth so much because a network's value grows with the number of things it connects. A rough rule known as Metcalfe's law says it grows with the square of the number of users.
Older documents call routers gateways. Today "gateway" usually means something else: an application-layer gateway that connects two different systems for one application, such as an email gateway. The one place the old meaning survives is the default gateway, the router a host sends packets to when it has no more specific route.
Designing applications
Networked programs follow a few standard designs.

Client/server
Most applications have a server that offers a service at a known name and port, and clients that connect to it: web browsers and web servers, mail clients and mail servers, apps and their APIs. "Client" and "server" name roles of programs, not kinds of hardware. One "server" machine typically runs many server programs, and your laptop runs servers too.
Servers come in two basic styles:
- An iterative server handles one request at a time: wait for a request, process it, send the response, repeat.
- A concurrent server starts a new thread, process or task for each client, or uses an event loop that interleaves many clients, so one slow request doesn't hold up the others.
The difference shows up as soon as clients overlap. In the lab, six clients connected within 50 ms of each other to two servers that each took 0.5 seconds to handle a request. Each line lists the six clients' response times in seconds, in the order they connected. First the iterative server, then the concurrent one:
0.55 1.04 1.53 2.02 2.51 3.01
0.53 0.53 0.53 0.53 0.53 0.53

With the iterative server, each client waited for everyone ahead of it. The sixth waited six times as long as the first. With the concurrent server, every client got the same quick response. A client can't tell which kind of server it's talking to except by timing, which is why the iterative/concurrent distinction is about servers, not clients. Nearly every real server is concurrent. Modern ones mostly use event loops (such as Linux epoll) or a fixed pool of worker threads rather than one thread per client, so they can serve tens of thousands of clients at once.
Peer-to-peer
In peer-to-peer (P2P) applications there's no single server. Each program, called a peer, is both client and server, and may pass requests on to other peers. The peers' connections form a network of their own, layered on top of the Internet, called an overlay network. BitTorrent, many blockchain networks and parts of video-calling systems work this way.
P2P's hardest problem is discovery: how does a new peer find others when peers come and go all the time? The usual answer is bootstrapping. The program starts with the addresses of a few peers or servers that are likely to be running, and learns about the rest from them. Many systems then use a distributed hash table (DHT) so peers can find which peer holds what without any central list. A second hard problem is that most peers sit behind NAT (network address translation, the address sharing done by home routers), which blocks incoming connections. Article 36 explains the techniques P2P programs use to get through.
The sockets API
Whatever the design, a program asks the operating system to do the networking through an application programming interface (API). By far the most common is the sockets API (Berkeley sockets), first released in BSD (Berkeley Software Distribution) Unix in the early 1980s. It's now part of POSIX, the standard interface for Unix-like systems, appears on Windows as Winsock, and sits underneath the networking libraries of every popular language.

The core calls for TCP:
- Server:
socket()creates an endpoint,bind()claims a local address and port,listen()marks it as accepting connections, andaccept()waits for a client and returns a new socket for that connection. The listening socket stays open for the next client. - Client:
socket(), thenconnect()to the server's address and port. That's when TCP performs its handshake and the operating system picks the ephemeral port. - Both:
send()andrecv()move bytes, andclose()ends the connection.
UDP is simpler: no listen(), accept() or connection, just sendto() and recvfrom() with an address each time, as in article 1's lab. Later articles mention socket options when they matter, such as TCP_NODELAY (article 68) and SO_KEEPALIVE (article 71). The IPv6 extensions to the API are standardized too, in RFC 3493 and RFC 3542, and getaddrinfo() is the modern way to write programs that work the same over IPv4 and IPv6.
Common questions
Can two programs listen on the same port?
Not with the same protocol, address and port, normally: the second bind() fails with "Address already in use". They can if they bind different addresses (one on 192.0.2.1:80, another on 198.51.100.1:80), or if both set SO_REUSEPORT, which Linux uses to spread incoming connections across several worker processes. TCP port 80 and UDP port 80 never clash.
Does a server need one port per client?
No. Every client connects to the same listening port. Each connection is told apart by its 4-tuple of addresses and ports, so one port can carry hundreds of thousands of connections.
Why does my server work on port 8080 but not 80?
On Linux, ports below 1024 need root or the CAP_NET_BIND_SERVICE capability, unless net.ipv4.ip_unprivileged_port_start has been lowered (as Docker does inside containers). Run the server with that capability, put a reverse proxy (a server that accepts requests and forwards them to yours) in front, or redirect 80 to 8080 with a firewall rule.
Is a port the same as a socket?
No. A port is a number in a header. A socket is an operating-system object a program uses to communicate, and it's bound to an address and port. One port can have many sockets: a listening socket plus one socket per accepted connection.
Why do I see ::ffff:192.0.2.10 in my server logs?
Your server uses a dual-stack IPv6 socket, and an IPv4 client connected. IPv4 clients on such sockets appear as IPv4-mapped IPv6 addresses, ::ffff: followed by the IPv4 address. Article 11 covers these special address forms.
Is DNS part of IP?
No. DNS is an ordinary application protocol that runs over UDP and TCP (and now also over TLS, HTTPS and QUIC, as article 55 explains). IP routes by address only, and never sees a name.
Is peer-to-peer the opposite of client/server?
Not really. Every individual connection in a P2P network still has one side that initiates it, the client, and one that accepts it, the server. The difference is that every peer plays both roles, and there's no central server holding the data.
Summary
- Port numbers (0–65535, separate for TCP and UDP) deliver each segment to the right program. IANA divides them into system (0–1023), user or registered (1024–49151) and dynamic (49152–65535) ranges.
- Clients get ephemeral ports chosen by the operating system: 32768–60999 on Linux, 49152–65535 on Windows and macOS. In the lab, Linux gave even ports to
connect()and odd ports tobind()with port 0. - A connection is identified by its 4-tuple, so one server port carries many connections and one client port can be reused for different destinations.
- Ports below 1024 traditionally need root. Today that's a convenience setting, not security.
- DNS maps names to addresses as an application-layer service. Programs call
getaddrinfo(), which lists IPv6 first when IPv6 works. - Internets, intranets and extranets use the same protocols with different access. Applications are mostly client/server, sometimes peer-to-peer, and servers should be concurrent: in the lab an iterative server made the sixth client wait 3.0 seconds instead of 0.5.
- The sockets API (
socket,bind,listen,accept,connect,send,recv,close) is how nearly every program uses TCP/IP.
Practice questions
1. A laptop opens two connections to the same web server's port 443, and a third to a mail server's port 993. Which values must differ between the first two connections, and could the third reuse one of their local ports?
Answer
The first two share the source address, destination address and destination port, so their source ports must differ. The third goes to a different destination address and port, so its 4-tuple is unique even if it reuses one of the first two's local ports. Whether the operating system actually reuses a port depends on its port-selection method.
2. ss -tln shows a socket at 127.0.0.1:5432. Can another computer on the network connect to it?
Answer
No. The socket is bound to the loopback address, so it only accepts connections made to 127.0.0.1 from the same host. To accept remote connections it would have to bind 0.0.0.0, [::] or one of the host's real addresses.
3. Which of these ports would you expect to see as a client's source port on a Windows PC: 443, 3306, 50021, 61500?
Answer
50021 and 61500, which are in Windows' default ephemeral range of 49152–65535. 443 and 3306 are server ports for HTTPS and MySQL.
4. An iterative server takes 200 ms per request. Ten requests arrive at the same moment. How long does the last client wait, ignoring network delay? And with a concurrent server that has enough CPU?
Answer
Iterative: the last client waits for nine requests ahead of it plus its own, 10 × 200 ms = 2 seconds. Concurrent: all ten are handled in parallel, so each waits about 200 ms.
5. Why do IPv6 addresses in URLs go in square brackets?
Answer
Because IPv6 addresses contain colons, and a URL uses a colon to separate the host from the port. The brackets mark where the address ends, as in http://[2001:db8::1]:8080/.
6. A peer-to-peer program starts on a new computer with an empty list of peers. How can it join the network?
Answer
By bootstrapping. It comes with, or downloads, the addresses of a few peers or servers that are usually online, connects to them, and learns about other peers from them. Many systems then use a distributed hash table to find which peers hold what.
Previous: Layering and Encapsulation: OSI, the TCP/IP Model and How Headers Nest
Next: Who Defines the Internet: RFCs, the IETF, IEEE and Real Implementations

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.