Detailed Explanation of TCP Port Numbers and Sockets
I. Knowledge Description
TCP uses port numbers to identify different application processes on the same host, while a Socket is a unique identifier formed by the combination of an IP address and a port number, used to distinguish different communication endpoints in a network. Understanding port numbers and sockets is fundamental to grasping TCP/UDP multiplexing, connection establishment, and network programming.
II. Role and Classification of Port Numbers
-
Why are Port Numbers Needed?
- An IP address can only identify a host, but a single host may run multiple network applications simultaneously (e.g., browser, email client).
- Port numbers (16-bit integers, range 0~65535) are used to distinguish between different processes within the same host, enabling multiplexing.
-
Port Number Classification
- Well-Known Ports (0~1023): Assigned to system-level services (e.g., HTTP-80, HTTPS-443, SSH-22).
- Registered Ports (1024~49151): Assigned to user-level applications (e.g., MySQL-3306).
- Dynamic/Private Ports (49152~65535): Ports temporarily used by clients (automatically assigned by the operating system).
III. Definition and Composition of Sockets
-
What is a Socket?
- Socket = IP Address + Port Number, e.g.,
192.168.1.10:80. - In TCP communication, a connection is uniquely identified by a pair of sockets:
Example:(Source IP, Source Port, Destination IP, Destination Port)(192.168.1.10:5000, 203.0.113.5:80).
- Socket = IP Address + Port Number, e.g.,
-
Role of Sockets
- The operating system uses sockets to correctly deliver received data packets to the corresponding application process.
IV. Example of Port Number Allocation in a TCP Connection
Taking a client accessing a web server as an example:
- The client randomly selects a dynamic port (e.g., 5000) as the source port.
- The server uses the well-known port 80 as the destination port.
- The connection is identified by the quadruple:
(Client IP:5000, Server IP:80). - When the server serves multiple clients simultaneously, the destination port for all connections is 80, but the source IP and source port differ, thus avoiding confusion.
V. Representation of Sockets in Programming
- Server-Side Process
- Create socket → Bind IP and port (e.g., 0.0.0.0:80) → Listen for connections → Accept client connection (generate a new socket to handle data).
- Client-Side Process
- Create socket → Connect to server (source port automatically assigned) → Send/Receive data via the socket.
VI. Analysis of Common Issues
- Port Conflict
- If two programs attempt to bind to the same port simultaneously, an "Address already in use" error occurs.
- Impact of TIME_WAIT State
- The party actively closing the connection (e.g., the client) enters the TIME_WAIT state, causing the port to be temporarily unavailable, but this only affects the reuse of the same connection quadruple.
VII. Summary
- Port numbers are process-level addresses; sockets are identifiers for network communication endpoints.
- TCP connections are uniquely determined by quadruples, enabling precise data distribution for many-to-many communication.