So welcome back, guys.
In this lecture, we will try to understand the concept of sockets.
We will see how sockets can be used to carry out inter-process communication between the processes.
The communicating processes can be processes which are running on the same machine, or they can be
processes which are actually running on different physical machines and communicating over the network.
So we will start with our first IPC technique that is sockets.
So Unix or Linux like operating system provide an interface called socket interface to carry out communication
between various types of entities.
Now here entities means the processes which wants to communicate with each other.
The socket interface are a bunch of socket programming related APIs.
So we will discuss the bunch of socket programming APIs or functions, which is provided by the Linux
operating system.
And the developers uses these APIs in order to implement socket based communication in their applications.
Two socket interface is nothing, but it is a collection of APIs or functions.
We shall be using these APIs to implement sockets of various types.
Right.
So sockets are actually a generic mechanism to implement a communication between processes.
So in this course we shall learn how to implement two types of sockets.
The first is the Unix domain sockets.
These type of sockets are used to implement or carry out inter-process communication between processes
which are actually running on the same system or same machine.
So such type of sockets are called Unix domain sockets.
The other type of sockets are network sockets.
These sockets are used to carry out communication between processes, but these processes are actually
running on different physical machines and these different physical machines are deployed over the network.
In fact, the socket interface can also be used to implement various other type of communications,
but those are out of the scope of our course.
In this module we will discuss the implementation of Unix domain sockets and network sockets.
So remember, socket interface are the bunch of APIs, and these APIs can be used to implement Unix
domain sockets or network sockets or any other type of sockets.
The only difference is that how we actually use these APIs to implement a specific type of socket communication.
So let us first cover how sockets work in general.
So in general means we will going to discuss that how actually sockets work on a Linux system.
It is important that you know that how sockets work internally.
And then we will see how socket APIs that is socket interface can be used to implement a specific type
of communication.
Now where a specific type of communication means either Unix, domain socket based communication or
network socket based communication.
So let us first build some background on sockets.
So going forward, you can see that here are the steps listed and we need to follow these steps in the
same sequence as they are listed in order to implement a socket based communication.
So we will discuss these steps one by one in detail, and we will learn how to implement a socket based
communication.
You can see that there are some function calls or APIs which are highlighted in a color.
These APIs together are called socket system calls.
Or socket interface, right.
So using these APIs, we will going to implement different types of socket based communication that
is Unix domain sockets as well as network sockets.
For each of these API, we will discuss that.
What is the purpose of each of these APIs and when to use and how to use while implementing socket based
communication?
So before diving into these steps, it is important that we study how socket based communication state
machine works and various socket APIs provided by Linux operating system.
So let us have some high level understanding regarding how socket based communication actually works
on the Linux operating system.
That is, we will discuss the state machines of socket based communication, the related APIs, the
necessary system calls that is provided by Linux operating system, which facilitates us implementing
socket based communication.
And once we get familiar with the concepts of socket based communication, then we will walk through
the example of implementing a client and server process which communicate with each other using socket
system calls.
Our socket interface.
So next, let us try to understand that, how these socket APIs actually fits in the computer architecture
diagram.
So here you can see that we had a discussion on computer layer architecture in the beginning of this
course, that our computer architecture is actually roughly divided into three layers.
The hardware layer and the top of the hardware layer is the operating system or kernel, and the topmost
layer is the application layer.
Right now.
You can see that in between application layer and the operating system.
Sandwich is the system call interface.
So this is actually broadly speaking, it is actually called a socket layer, right?
Or system call interface.
So you can see in this system call interface layer, there are a bunch of APIs like socket, accept,
connect, send to receive from close, select and etcetera.
These APIs are actually called system calls.
And what is the purpose of these system calls?
These system calls are invoked by the application.
So by invoking these system calls, the applications actually ask the operating system for its service.
For example, the application may invoke a system call and ask the operating system that, hey, I want
X, Y, Z service operating system in response to the system call invocation, provide a service to
the application.
So so you can see here, application is like a master and the operating system acts like a slave applications
orders the operating system to provide the application some service and this order is done using system
call interface APIs.
So Linux provides a set of APIs called System Calls, which application can invoke to interact with
the underlying operating system.
Socket APIs are the interface between application and operating system using these APIs.
Application instructs the operating system to provide its service.
So let us take a very simple example which you must have used already.
Even if you don't know that you are using actually system call.
So familiar example is the malloc and free.
If you have ever written a single C program then you must have used malloc or free function.
So these are actually system calls and you make use of the system calls in your program.
So the moment your program calls a malloc, it means your program is asking the operating system to
give you a program, a chunk of memory.
So this is the example where your application is requesting a service from the operating system using
system call called malloc.
Right.
And similarly, another system call is a free system call, using which your application asks the operating
system to reclaim back the memory.
So from this example, we clearly understand that system calls are actually a set of APIs which is invoked
by the application to demand a service from the operating system or kernel.
Right.