Ports and Sockets
An application process running on one computer that wants to communicate with an application process running on another computer identifies itself using a 16-bit port number, which is subsequently used by the transport layer protocol (TCP or UDP) to deliver incoming messages. Common server applications such as Telnet and FTP use one or more of the well known port numbers (these range between 1 and 1023). Most server applications only use one port, although some (like FTP) use two. The use of a specific port number by server applications allows the client process to send a request to a server without having to first find out which port is being used by the server application. HTTP requests, for example, are addressed by default to port 80 on the server. The clients themselves do not need to use a well known port, since they are initiating the communication. A client process is dynamically allocated a port number (in the range 1024 to 65535) by the client operating system. This number is subsequently included in all datagrams sent to the server.
Common server applications listen for incoming service requests on "well known" ports
A socket is essentially an addressable end point in a communication between two processes, and consists of a unique combination of IP address, port number and transport layer protocol (usually TCP). When a client application wishes to communicate with a server application, the operating system creates a socket which is then used by the client application to receive incoming data from the server. The unique combination of transport protocol, IP address and port number allows the communication end point to be addressed by a process running on a remote server, and ensures that data is delivered to the process for which it is intended. The server application will have its own socket for communicating with the client, and a connection is established between client and server using the two socket addresses. The applications exchange information by writing to, or reading from, the sockets they have created.
The connection used by a client process consists of two sockets, one at each end of the connection. The connection can thus be identified by a unique combination of four numbers - the source and destination IP addresses, together with the source and destination port numbers. It is possible for several client applications running on different computers to connect to the same destination socket on a server. The diagram below shows three computers establishing Telnet sessions with a server, all using destination port 23. Datagrams sent in reply by the server contain the socket address for the client application which includes the client computer's unique IP address, so there is no confusion as to which computer a datagram is destined for, even if the source and destination ports are the same in each case.
Multiple clients connecting to a single destination port
Using sockets, it is even possible for several client applications running on the same computer to connect to the same destination socket on a server. The diagram below shows two processes on the same computer establishing FTP sessions with a server using destination port 21. Datagrams sent to the client by the server contain the socket address for each client process, which includes the client process's individual port number, so there is no confusion as to which process a datagram is bound for, even though in this example the source and destination IP addresses and the destination port number are the same for each process.
Two processes on the same client connecting to a single destination port