Pretty Little Things: TCP/IP, Ports and Sockets

In this blog post, I am going to write a little bit about TCP/IP, ports and sockets. These are those little things, as a software developer, which we keep hearing about, but same time get confused by an overwhelming information on the internet. The main goal of this post is to have a simple reference to these topics and how they work together in IT stack.

This image has an empty alt attribute; its file name is image.png

Ethernet

for most part, we need some type of mechanism to actually move data from one device to the next. Ethernet is the protocol which allows us to do that (i.e. move traffic from device to network). There are a lot of other protocols and components involved and a simple internet search should give you more information . However for the sake of simplicity we are going to talk about IP, TCP and HTTP, these are protocols which are used heavily in networking infrastructure for communication.

This image has an empty alt attribute; its file name is image-1.png

we have IP (internet protocol) that allows to find devices wherever they are on the internet. We have TCP that allows us to setup our session and we have HTTP that allows us to actually send data from one device to the other. These protocols must be orchestrated in this exact method.

Ethernet is what we encapsulate our entire message including our IP information, our TCP information and our HTTP information.

OSI Model

Here is a simple overview of OSI Model and corresponding protocols that operate there. OSI model is another great example of Separation of Concern principle, the idea of separating responsibilities in various layers, allow us to build systems which are easy to reason about.

This image has an empty alt attribute; its file name is image-2.png
This image has an empty alt attribute; its file name is image-3.png

Surfing the Web

Lets see all these with a simple example, “surfing the web”.

This image has an empty alt attribute; its file name is image-9.png

When we are surfing the internet, we open a browser on our computer and enter e.g. http://www.google.com.

This image has an empty alt attribute; its file name is image-10.png
  • We are using HTTP/HTTPS protocols to access the website.
  • So, the protocols we use to accomplish this are very specific and precise and order with which they’re used is paramount to their operation.
  • So, HTTP is a specific protocol designed exclusively for a WebClient to ask a WebServer for a website.
  • But HTTP needs some additional tools to perform this. First we need to setup session between our PC and the Server, so that we can transfer data b/w them using a protocol called TCP (Transmission control protocol)
This image has an empty alt attribute; its file name is image-11.png
  • TCP is going to do a three-way handshake, Syc, Syn-Ack, Ack.
  • Syn, Syn-Ack, Ack here are setting up a session between our client and the server.
  • So, we can’t use HTTP until we have a TCP session established. HTTP won’t work until TCP is connected.
  • TCP can’t be setup until we know exactly where our server exists (e.g. URL address). So, our client has to know precisely where the web server is. In order to do that, we use another protocol here and that is IP.
  • So IP means, that we will assign globally unique addresses to each device on internet and then we can have routing tables on internet to find these devices. (routing tables…, this idea resembles Angular Router, ClientSide Routing, Web Api Routing, so we have similar thing going on here)
  • So, if we have communication path to these devices, then we can setup our Syn, Syn-Ack, Ack with TCP, which then allows to talk HTTP. So, these protocols are built on top of each other.
This image has an empty alt attribute; its file name is image-12.png

Purpose of TCP

  • Established a connection b/w source and destination network nodes.
  • Provides reliable delivery of data over an IP network (IP is an unreliable network protocol, TCP achieve reliability with acknowledgements)
  • Handles data to/from higher layer applications.
  • Manages a connection b/w source and destination network nodes.

TCP/IP Ports and Sockets

  • A network port is a process-specific or an application-specific software construct serving as a communication end-point.
  • on a TCP/IP network every device must have an IP address.
  • The IP address identifies the device e.g. computer.
  • However, an IP address alone is not sifficient for running network applications, as a computer can run multiple applications and/or services.
  • Just as the IP address identifies the computer, the network port identifies the application or service running on the computer.
  • The use of ports allow computers/devices to run multiple services/application.
  • IP Address + Port Number = Socket
  • House or Apartment block analogy
    • IP address corresponds to the street address
    • All of the apartments share the same street address.
    • However, each apartment also has an apartment number, which corresponds to the port number.
  • A connection between two computers uses a socket.
    • A socket is the combination of IP Address + Port.
    • Each end of the connection will have a socket.

Transport Layer Addressing (Port Numebrs)

  • Port numbers range is 0 – 65,535. its a 16-bit value.
  • We divide these into two categories
    • Server Port Numbers
      • Well known / registered port numbers (0 to 1,023 / 0,024 to 49,151)
    • Client Port Numbers
      • Ephemeral port numbers (temporary numbers) (49,152 to 65,535)
This image has an empty alt attribute; its file name is image-19.png
This image has an empty alt attribute; its file name is image-20.png

Socket

This image has an empty alt attribute; its file name is image-16.png

A unique TCP session will have what’s called a unique Socket. Where a socket is defined by four pieces of information i.e.
Source IP Address
Source Port Number
Destination IP Address
Destination Port Number

This image has an empty alt attribute; its file name is image-17.png

Having unique port number for each connection allows my desktop (client) to have multiple sessions with same server.

This image has an empty alt attribute; its file name is image-18.png

As a socket is a combination of your IP address and the Port number, if two clients happen to choose the same port number, they still create a unique socket, and in this way multiple clients can also connect to the same server.

Some Commands to Check the Information

 ipconfig /all  

Shows all of the interfaces and we can also see IP addresses etc.

netstat -ano

Shows all the listening ports

This image has an empty alt attribute; its file name is image-15.png

.NET Core Web Application

If we run a simple .NET core application on console, you will see following information. This should now be clear when we understood what actually a port is.

This image has an empty alt attribute; its file name is image-21.png

A development Node Server

Following is the screen shot of node based development web-server and it is configured to use port 3000.

This image has an empty alt attribute; its file name is image-22.png

I believe, you have used these ports a lot even without knowing it, In one of the next blog post, I will try to show you how to use programming languages to implement these type of communications and utilize these protocols.
This post was mostly theory intensive but that is now out of the way and we can focus on development tasks in next days.
Feel free to ask, if something is not clear. Looking forward to hearing from you.
Till then Happy Coding!