This article explains the network socket in an easy way
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
The definition explains socket in basic terms but to get a crystal clear picture of it, let's understand this with the help of an analogy.
Think you are in your house due to lock down and you are prohibited to go out of your house, Let's build on this. one day since you were super bored you decided to have a chat with your neighbor, we will assume your house has a window. ( No! you don't jump from it).
In order to have a chit chat with your neighbor you go to the window and wave hello. Luckily, your neighbor is standing right on the window and he has his window open. he waves back and you guys start communication.
Let's compare it with our definition of network sockets
- Here, House is analogous to a computer.
- The window we are talking about is like a socket, an endpoint from where you can send messages or receive one
- the conversation you are having is data.
A socket is one endpoint of a two-way communication link between two programs running on the network.
Like in our example windows are endpoints. if we close the windows or don't have windows, then we won't be able to transfer our messages. (oh please! you are not allowed to call.)
A socket is bound to a port number so that the Network layer can identify the application that data is destined to.
Let's also assume that the house has 6 rooms and there are 6 family members one in each room. All the rooms have a window and a number (1 to 6). Now if a neighbor wants to talk to one of the family members they will have to specify room number as well.
Similarly, on a computer, there can be many applications running. Each socket must be attached to a port ( which will represent the application ). This way Network layer knows where to send incoming data to.
An endpoint is a combination of an IP address and a port number.
With the help of port, we only know which application to send data to, but in a network, there could be many computers hence we need an address to uniquely identify the host we want to talk to.
In our analogy, there could be many houses. In order to talk to someone, you need their house address and the window number you want to talk to.
An IP address is an address assigned to each device within a network to be able to uniquely identify it. It can be of the form
172.16.254.1 in IPv4, and
2001:db8:0:1234:0:567:8:1 in IPv6. you can read more here
IP refers to internet protocol. It is a set of rules that dictates how the communications should happen in a network.
Let's look at the whole flow step by step.
- Your application can listen to the messages or send messages to or from the network by creating a socket.
- In order to create a socket, we need to bind it to a port.
- Someone in a network wants to communicate. They need an IP address ( to uniquely identify our device) and a port number( the port we have bound our socket to or basically where the application is listening ).
- They send a message to us. The network first recognizes the IP and tries to find the device from it. it sends the message to the device.
- The device has a network layer that looks at the incoming messages from the internet. It receives the messages and parses the port number and passes it to that socket.
- The socket receives the message and the application which created the socket, now has access to the message and everyone is happy.
The flow depicted here is an ideal flow. There are many other protocols involved like how the message will be packed and how they will be routed. How will the sender know if the package is received or is lost somewhere in between? For now, let's look at TCP or UDP.
Once we receive messages there has to be a way to pass them to the application. We have another layer that sits in between the application and the network layer which is called the transport layer. It is referred to as the transport layer because it handles how the packages will be sent to the application from the network layer and vice versa. This is required because at a point there could be multiple sockets open and a lot of data that the device must be receiving. Its job is to collect all the packets meant for sockets and transfer them respectively.
There are two basic protocols for this:
TCP: Transmission Control Protocol is a connection-oriented protocol i.e it ensures that a proper connection exists before the communication can begin. Think of it as you want to talk to the neighbor but you will only start a conversation once you are sure they are able to listen.
UDP: User Datagram Protocol is a connectionless protocol that does not care for a proper connection which makes it fast but less reliable. For example, you might have experienced game lags that because some of the messages were lost. Most Real-time communication and VoIP are done through UDP.
Now since there are two types of protocols you also must specify which protocol you want to use that why the socket you will create will either be a UDP socket or a TCP socket ( I am lying there are more!!). basically a listening socket looks like
(Please do not try to hack my pc).
Here we can see the protocol in the first column and the port along with IP in the 4th column.
I think by now you would have got a clear idea about network sockets and other basic concepts. we have just scratched the surface there is a lot you can know. I would encourage you to read more about the TCP/IP model and the OSI model. Learning about TCP and IP in detail once.
Feel free to ask any questions or in case you want to have any discussion do comment below.