What are port numbers
We had already learned about IPv4 addresses and MAC addresses in previous lessons. We also learned that IPv4 addresses (layer3 addresses or logical addresses) belongs to layer 3 (Network layer) of TCP/IP protocol suite and MAC addresses (hardware address or physical address) belongs to layer 2 (Datalink layer) of TCP/IP protocol suite.
In other words, IPv4 addresses are used as the addressing system at layer 3 (Network layer) and MAC addresses are used as the addressing system at layer 2 (Datalink layer) of TCP/IP protocol suite. Similarly, ports are the addressing system used at layer 4 (Transport layer) of TCP/IP protocol suite. So, what is the use of these port numbers? Keep reading.
A computer is able to run multiple network applications (or processes) at the same time and an IPv4 address alone cannot distinguish between different network applications running on the computer. So, TCP/UDP port numbers provide an address for the network application/processes running in your computer.
When you are using a browser, you can use multiple tabs to connect to different websites. The data displayed in different tabs are coming from different web servers. IPv4 address can uniquely identify the network interface of the computer and TCP/UDP ports uniquely identify the network applications/processes running on a computer.
Please refer the TCP and UDP header formats below. Port numbers are 2 bytes (16 bits) in length. So the maximum possible value for port numbers in decimals is 65535 (-1).
To understand the concept of port numbers clearly, refer two images below. Below images shows five layers of TCP/IP protocol stack running on a computer at the left side and some servers running on internet cloud at right side. There are three applications running at layer 5 (Application Layer). One is a browser program running plain HTTP, second one is another browser running HTTPS and third one is an email client program for sending email via an email server using SMTP.
Let us consider the IPv4 address is the only addressing system available in TCP/IP protocol stack. If IPv4 address is the only addressing system available, how can different applications receive exact network data they are expecting from their network partner at the other end? So, we need an addressing system for applications/processes also. Port numbers provide us an addressing system for network applications/processes. Applications/processes those need a network connection bind with a TCP/UDP port number, depending on which protocol they are using at layer 4 (TCP or UDP). That port number becomes another address for network applications/processes at layer 4.
In this example, the Email client has TCP 55000 as its port number, HTTPS client has TCP 55001 has its port number and HTTP client has TCP 55002 as its port number at the client side. Port numbers at client side are uniquely and dynamically assigned based on availability. At the server side, all are well-known ports. HTTP server process is listening at its well-known port number 80, HTTPS server process is listening at its well-known port number 443 and SMTP server process is listening at its well-known port number 25.
What are socket numbers
Okay, now we know what are IPv4 addresses and port numbers. The combination of an IPv4 address and a port number is known as the socket number. A pair of sockets, one socket at the client side and other socket at the server side, define the TCP/UDP connection end points. A socket number can uniquely identify a network resource in the whole internet.
For example; let us consider 188.8.131.52 as the IPv4 address of the client and 184.108.40.206 as IPv4 address of the HTTPS server for the image shown above. Socket number for HTTPS connection at client side is 220.127.116.11:55001 and server side is 18.104.22.168:443.
What are well-known ports and why we need well-known ports
Well-kown ports are the port numbers those are assigned to standard protocols/applications. The reason why we need well-known ports is, if we know the IPv4 address of the server, then we can expect at which port the server process is listening. If the server process picked a random port number instead of its well-known port number (for example if HTTP server process had selected TCP port number 57998 instead of its well-known port 80) clients may not be able to guess it.
What are dynamically assigned port numbers
Dynamically assigned port numbers are allocated to applications/processes dynamically when required. Dynamically assigned ports are allocated to applications/processes uniquely from the top range of port numbers.