Port vs Socket

Nilesh Deokar
5 min readFeb 21, 2020

--

In networking this is basic concept and even after consuming apis from server, developers still struggle to grab this nuance. In this post I would try to explain difference.

source : lonestar.edu

Port :

is a 16 bit identity, used for uniquely identifying a process on a Host.
My first encounter to port was :
http://localhost:8080/phpmyadmin

‘8080’ is traditional port for running own web server. If I want to host a HTTP web server I could use any value upto 65535. Why 65535 ?? Since port is a 16 bit unsigned integer identity giving you 2¹⁶ : 65535 numbers. In reality ports from 1 to 1023 are accessible only to root user.
Q : Why not 65536?

Let’s try to understand this by experiment. I am going to use python’s SimpleHTTPServer which lists and transfers files present in current directory over HTTP. Type following command into terminal to start server :

python -m SimpleHTTPServer 8080

python -m SimpleHTTPServer 8080

Then get the ip address of the machine using ifconfig/ipconfig. Now visit ip:8080 using browser. My ip was 192.168.1.245 so final url becomes :
192.168.1.245:8080

SimpleHTTPServer response browser (Client)
Python SimpleHTTPServer running on PORT 8080 console

Notice following line printed on terminal :

192.168.1.245 — — [24/Nov/2019 02:47:38] “GET / HTTP/1.1” 200 -

This shows connection request is coming from ip 192.168.1.245 making a simple HTTP GET request and 200 OK response is sent back. IP is same for server and client since both are on same system.

Now make another request using cellphone or other machine connected to same network/wifi (VM would also work). I am using my cellphone to make a request to python server. Python server url remains same : 192.168.1.245:8080

Notice ip address is changed for last two request, this is the ip address of cellphone.

192.168.1.251 — — [24/Nov/2019 02:54:15] “GET / HTTP/1.1” 200 -

Okay, So learning so far is python server is able to identify requests from different machines (clients).

Now let’s assume that we are able to simulate a situation when, requests from two machines (clients) hits python server exactly at same time.

How does the python server uniquely identifies each requests and gives back appropriate response ??
Answer : This is where Socket comes in a picture.

Socket

Socket is an identifier for uniquely identifying connection on a PORT.
Even though every machine (client) is using same ip:port (server)to request data, upon receiving a connection request server OS assigns a unique identifier to a connection aka socket and that’s how python server identifies each connection uniquely.

Let’s build a simple server using C lang and see how server differentiate between multiple connections. So our aim is to accept simultaneous connections from clients on same Port. Here is a link to a server which I have written. Download file to your local machine and then open terminal and
Compile using gcc -Wall -o apache Apache_Server.c
Run using ./apache

Apache server start

You should already have an ip address of a machine from python experiment. Use that ip and port 8080 to hit the Apache server. In my case url becomes
http://192.168.1.245:8080/ visit this url using browser.

Response from Apache connection 1

Line 1 prints 4 pieces of a connection, ip:port of client and server.
Line 2 prints socket id : 4 for that client.
In response above we have Port value and socket value. Now let’s try to hit server from different tab to create simultaneous connection.

Response from Apache connection 2

Notice Client socket id : 5 Apache is still listening on same port : 8080 but still able to identify this connection distinctly.

This part is vital in understanding socket.
After accepting a connection on a port, OS assigns a socket to it. Then OS passes socket identifier to an application. If you look at source code of Apache Server, socket id is a result of :

accept() in Apache_Server.c

Every-time new connection is accepted. OS assigns new client file descriptor to it AKA Socket. ( Why file descriptor ?: Everything is file in linux. )
Refer the following diagram.

Generic view of Port and Socket

If you are further interested in how Apache_Server works, Have a look at documentation of : socket(), bind(), listen(), accept() and their usage in Apache_Server.c

Conclusion :

Socket is managed by Operating System and hence is an Application layer component. On the other hand Port is an external identity and used with IP to find a source/destination machine on the network. Hence Transport layer component.

Web server has many services running on it’s machine, Port gives a way to uniquely identify a service on a server. Each service can be represented with port number. Popular ports with their services :

  1. HTTP : 80
  2. HTTPS : 443
  3. FTP : 21, 20
  4. SMTP : 25

Socket: It gives a way to uniquely identify a connection accepted on a port. Socket usually consist of three things :

  1. IP address
  2. Port
  3. Protocol (TCP/UDP)

(52.47.209.216 , TCP , port 8080) is a socket.

Further reading :

Thank you for reading!

Tap the ❤ button if you found this article useful!

--

--

Nilesh Deokar

Android Associate | Geek-ish | Crypt0gr4phy | UX Enthu