What is the difference between a port and a socket?
Useful first: IP addresses · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALDistinguish an IP address, a transport port, and the two common meanings of socket.
Separate the address, the number, and the programming object.
A port is a number within a transport protocol
An IP address is part of the network destination. A TCP or UDP port helps the receiving system direct traffic to a transport endpoint there. A port is a number carried in protocol headers, not a physical connector.
[1] [4]Keep the transport protocol in the label
TCP port 443 and UDP port 443 are separate transport cases, even at the same IP address. A familiar port number is also no proof of which application actually sent the traffic or whether it is trustworthy.
Socket can mean an endpoint or a local resource
The TCP specification calls an IP-address-and-port pair a socket. Programming APIs also use the word for an operating-system resource, exposed through an object or handle. A Python socket object is therefore not just a port number, and its numeric descriptor is not its network port.
[1] [3]Read the protocol and address alongside the port; distinguish that endpoint from the object your program holds.
A socket is another name for a port number.
A TCP endpoint includes an address as well as a port. A programming socket object is a local resource used to perform network operations.
Show the explanation
The transport protocols distinguish them; the number alone does not identify either application. Port numbers are interpreted in their transport context. Neither a matching number nor a registered service name proves which process or application is using it.
YOUR GOALExplain how one server port serves distinct connections and identify their local and remote endpoints.
Follow a listener and two accepted TCP connections.
Accept returns a connection socket
A typical TCP server binds a socket and starts listening. Calling accept() returns a new socket for an incoming connection. The original listening socket can keep accepting; the accepted socket carries that connection's data. These are different programming resources with different jobs.
A new socket does not require a new server port
Imagine two established connections to a server configured on TCP port 443:
A: 192.0.2.10:53120 → 198.51.100.20:443
B: 192.0.2.10:53121 → 198.51.100.20:443Both retain the server endpoint 198.51.100.20:443. Their different client endpoints distinguish the connections. The addresses are reserved for documentation, and the client ports are illustrative choices, not a prediction of an operating system's allocation.
Inspect both ends from the server
For connection A above, a Python IPv4 socket would report these endpoint values:
conn.getsockname() # ('198.51.100.20', 443)
conn.getpeername() # ('192.0.2.10', 53120)getsockname() reports the socket's own address; getpeername() reports its peer. This is an illustration of the documented API, not output from a live connection.
A listener accepts connections; each accepted socket represents a conversation with its own peer.
Every accepted TCP connection needs a different server port.
Accepted connections can share the server address and port while differing in their client address or port.
Show the explanation
Their client source ports differ: 53120 and 53121. The server address and port are unchanged. Combining each client endpoint with the server endpoint distinguishes these two TCP connections.
YOUR GOALBound five-tuple identification to its context and distinguish a connected UDP socket from TCP connection establishment.
Use connection labels without giving them extra guarantees.
The five-tuple includes direction and transport
A common label for traffic is (transport protocol, source IP, source port, destination IP, destination port). In A's client-to-server direction, it is (TCP, 192.0.2.10, 53120, 198.51.100.20, 443). The return direction swaps source and destination; it is still the same bidirectional TCP connection.
An endpoint pair is not a permanent identity
After a TCP connection ends, the same endpoint pair can identify a later connection. TCP calls these different incarnations and uses sequence-number rules and states such as TIME-WAIT to limit confusion with old traffic. Reuse is constrained, not necessarily immediate.
When interpreting a capture, keep the time and connection state alongside the tuple; do not treat that tuple as a permanent user or session identifier.
[1]UDP connect is not a TCP handshake
UDP sends datagrams without TCP's connection establishment. Its send and receive interface can include destination and sender addresses. It does not use the TCP listen-and-accept conversation model.
An API can still call a UDP socket connected. For example, Winsock connect() on a datagram socket sets a default destination and discards received datagrams from other addresses. That local association does not add a TCP handshake, guaranteed delivery, or ordered reliable transport.
A tuple labels traffic in context; a socket API operation does not change the guarantees of its transport.
Calling connect on a UDP socket turns it into a reliable TCP-style connection.
The API can associate a peer with the socket while the transport remains UDP. The name of the call does not add TCP guarantees.
Show the explanation
A default peer association, without TCP connection establishment or reliable ordered delivery. Winsock documents a default destination and peer filtering for a datagram socket. UDP still supplies datagrams without TCP's reliability; connect is not an application receipt.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- RFC 9293 — TCP sockets, connection identification, and connection incarnations
- RFC 768 — UDP datagrams, ports, and the user interface
- Python documentation — Socket objects, accept, getsockname, and getpeername
- RFC 6335 — Transport ports, session identification, and security limits
- Microsoft Learn — Winsock connect and connectionless sockets
- Microsoft Learn — Winsock accept and the listening socket
- RFC 5737 — IPv4 address blocks reserved for documentation