Search

What are port numbers, socket numbers, well-known ports and dynamically assigned ports

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.

computer-network-applications.jpg

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.

tabs-in-browser.jpg

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 ([216]-1).

tcp-header-port-number.jpg

udp-header-port-number.jpg

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.

tcp-port-outgoing.jpg

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.

tcp-port-incoming.jpg

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 1.1.1.1 as the IPv4 address of the client and 2.2.2.2 as IPv4 address of the HTTPS server for the image shown above. Socket number for HTTPS connection at client side is 1.1.1.1:55001 and server side is 2.2.2.2: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.

Related Tutorials
Introduction to TCP/IP, Features of TCP/IP, TCP/IP History, What is RFC (Request for Comments)
What is PDU (Protocol Data Unit)
How data is moved through different layers of OSI model at sending and receiving computers
Names of data packets at different layers of OSI model
Why OSI model failed
Four Layers of original TCP/IP model, Functions of four layers of TCP/IP model
Five layered TCP/IP model
Comparison between four layered TCP/IP model and five layered TCP/IP model
Protocols/Standards at various layers of TCP/IP model
Comparison between TCP/IP and OSI models
How data is moved through different layers of TCP/IP model at sending and receiving computers
TCP/IP Encapsulation and Decapsulation
Physical Layer (Layer 1)
Datalink Layer (Layer 2)
Ethernet Frame Format
What is MAC address or Layer 2 address or physical address
Network Layer (Layer 3)
IPv4 Protocol, IPv4 header and fields of IPv4 header
IPv4 addresses, IPv4 Address Classes, IPv4 Address Classifications
Transport Layer (Layer 4)
Transmission Control Protocol (TCP) Segment Header
TCP Three-way Handshake
Multiplexing and Demultiplexing using port numbers
Transmission Control Block (TCB)
TCP Sliding Window
TCP Connection Termination
User Datagram Protocol (UDP)
Differences between TCP and UDP
Application Layer (Layer 5)