WEBVTT

00:04.820 --> 00:06.350
So welcome back, guys.

00:06.350 --> 00:10.670
In this lecture, we will try to understand the concept of sockets.

00:10.670 --> 00:17.180
We will see how sockets can be used to carry out inter-process communication between the processes.

00:17.180 --> 00:23.300
The communicating processes can be processes which are running on the same machine, or they can be

00:23.300 --> 00:29.030
processes which are actually running on different physical machines and communicating over the network.

00:30.320 --> 00:34.700
So we will start with our first IPC technique that is sockets.

00:35.090 --> 00:44.150
So Unix or Linux like operating system provide an interface called socket interface to carry out communication

00:44.150 --> 00:46.670
between various types of entities.

00:47.270 --> 00:52.760
Now here entities means the processes which wants to communicate with each other.

00:55.340 --> 01:00.230
The socket interface are a bunch of socket programming related APIs.

01:00.320 --> 01:08.210
So we will discuss the bunch of socket programming APIs or functions, which is provided by the Linux

01:08.210 --> 01:09.290
operating system.

01:09.290 --> 01:18.500
And the developers uses these APIs in order to implement socket based communication in their applications.

01:19.980 --> 01:25.170
Two socket interface is nothing, but it is a collection of APIs or functions.

01:26.010 --> 01:30.900
We shall be using these APIs to implement sockets of various types.

01:31.230 --> 01:31.890
Right.

01:31.890 --> 01:39.120
So sockets are actually a generic mechanism to implement a communication between processes.

01:39.900 --> 01:44.580
So in this course we shall learn how to implement two types of sockets.

01:44.820 --> 01:47.250
The first is the Unix domain sockets.

01:47.280 --> 01:54.150
These type of sockets are used to implement or carry out inter-process communication between processes

01:54.150 --> 01:58.350
which are actually running on the same system or same machine.

01:58.470 --> 02:02.100
So such type of sockets are called Unix domain sockets.

02:02.490 --> 02:05.460
The other type of sockets are network sockets.

02:05.490 --> 02:11.910
These sockets are used to carry out communication between processes, but these processes are actually

02:11.910 --> 02:18.270
running on different physical machines and these different physical machines are deployed over the network.

02:18.840 --> 02:25.210
In fact, the socket interface can also be used to implement various other type of communications,

02:25.210 --> 02:29.050
but those are out of the scope of our course.

02:29.590 --> 02:35.560
In this module we will discuss the implementation of Unix domain sockets and network sockets.

02:36.270 --> 02:42.630
So remember, socket interface are the bunch of APIs, and these APIs can be used to implement Unix

02:42.630 --> 02:46.500
domain sockets or network sockets or any other type of sockets.

02:46.800 --> 02:55.260
The only difference is that how we actually use these APIs to implement a specific type of socket communication.

02:56.220 --> 03:00.030
So let us first cover how sockets work in general.

03:00.060 --> 03:06.420
So in general means we will going to discuss that how actually sockets work on a Linux system.

03:07.560 --> 03:12.690
It is important that you know that how sockets work internally.

03:12.690 --> 03:19.860
And then we will see how socket APIs that is socket interface can be used to implement a specific type

03:19.860 --> 03:20.960
of communication.

03:20.970 --> 03:27.000
Now where a specific type of communication means either Unix, domain socket based communication or

03:27.000 --> 03:28.890
network socket based communication.

03:29.400 --> 03:33.900
So let us first build some background on sockets.

03:35.860 --> 03:43.150
So going forward, you can see that here are the steps listed and we need to follow these steps in the

03:43.150 --> 03:48.940
same sequence as they are listed in order to implement a socket based communication.

03:51.040 --> 03:57.670
So we will discuss these steps one by one in detail, and we will learn how to implement a socket based

03:57.670 --> 03:58.780
communication.

04:00.240 --> 04:06.330
You can see that there are some function calls or APIs which are highlighted in a color.

04:07.110 --> 04:12.000
These APIs together are called socket system calls.

04:14.080 --> 04:18.730
Or socket interface, right.

04:18.940 --> 04:26.440
So using these APIs, we will going to implement different types of socket based communication that

04:26.440 --> 04:30.640
is Unix domain sockets as well as network sockets.

04:32.000 --> 04:35.300
For each of these API, we will discuss that.

04:35.300 --> 04:42.050
What is the purpose of each of these APIs and when to use and how to use while implementing socket based

04:42.050 --> 04:43.010
communication?

04:44.280 --> 04:51.780
So before diving into these steps, it is important that we study how socket based communication state

04:51.780 --> 04:57.030
machine works and various socket APIs provided by Linux operating system.

04:58.730 --> 05:04.790
So let us have some high level understanding regarding how socket based communication actually works

05:04.790 --> 05:06.470
on the Linux operating system.

05:06.770 --> 05:13.520
That is, we will discuss the state machines of socket based communication, the related APIs, the

05:13.520 --> 05:19.760
necessary system calls that is provided by Linux operating system, which facilitates us implementing

05:19.760 --> 05:21.320
socket based communication.

05:22.700 --> 05:28.490
And once we get familiar with the concepts of socket based communication, then we will walk through

05:28.490 --> 05:35.450
the example of implementing a client and server process which communicate with each other using socket

05:35.450 --> 05:36.620
system calls.

05:37.450 --> 05:39.040
Our socket interface.

05:40.540 --> 05:48.850
So next, let us try to understand that, how these socket APIs actually fits in the computer architecture

05:48.850 --> 05:49.540
diagram.

05:54.130 --> 05:59.230
So here you can see that we had a discussion on computer layer architecture in the beginning of this

05:59.230 --> 06:05.500
course, that our computer architecture is actually roughly divided into three layers.

06:05.650 --> 06:11.530
The hardware layer and the top of the hardware layer is the operating system or kernel, and the topmost

06:11.530 --> 06:13.240
layer is the application layer.

06:15.680 --> 06:16.790
Right now.

06:16.790 --> 06:20.510
You can see that in between application layer and the operating system.

06:20.630 --> 06:23.720
Sandwich is the system call interface.

06:25.030 --> 06:33.250
So this is actually broadly speaking, it is actually called a socket layer, right?

06:33.970 --> 06:35.980
Or system call interface.

06:37.130 --> 06:44.120
So you can see in this system call interface layer, there are a bunch of APIs like socket, accept,

06:44.120 --> 06:48.110
connect, send to receive from close, select and etcetera.

06:48.140 --> 06:51.890
These APIs are actually called system calls.

06:52.850 --> 06:55.310
And what is the purpose of these system calls?

06:55.340 --> 06:58.730
These system calls are invoked by the application.

06:59.090 --> 07:06.890
So by invoking these system calls, the applications actually ask the operating system for its service.

07:06.920 --> 07:14.270
For example, the application may invoke a system call and ask the operating system that, hey, I want

07:14.300 --> 07:21.710
X, Y, Z service operating system in response to the system call invocation, provide a service to

07:21.710 --> 07:22.670
the application.

07:22.670 --> 07:31.160
So so you can see here, application is like a master and the operating system acts like a slave applications

07:31.160 --> 07:39.480
orders the operating system to provide the application some service and this order is done using system

07:39.480 --> 07:41.490
call interface APIs.

07:43.220 --> 07:50.000
So Linux provides a set of APIs called System Calls, which application can invoke to interact with

07:50.000 --> 07:51.650
the underlying operating system.

07:52.940 --> 07:59.130
Socket APIs are the interface between application and operating system using these APIs.

07:59.150 --> 08:03.620
Application instructs the operating system to provide its service.

08:03.740 --> 08:09.110
So let us take a very simple example which you must have used already.

08:09.110 --> 08:12.470
Even if you don't know that you are using actually system call.

08:12.920 --> 08:15.760
So familiar example is the malloc and free.

08:15.770 --> 08:22.100
If you have ever written a single C program then you must have used malloc or free function.

08:22.100 --> 08:27.920
So these are actually system calls and you make use of the system calls in your program.

08:27.920 --> 08:35.660
So the moment your program calls a malloc, it means your program is asking the operating system to

08:35.660 --> 08:38.180
give you a program, a chunk of memory.

08:38.450 --> 08:45.140
So this is the example where your application is requesting a service from the operating system using

08:45.140 --> 08:47.270
system call called malloc.

08:48.900 --> 08:49.500
Right.

08:49.500 --> 08:56.610
And similarly, another system call is a free system call, using which your application asks the operating

08:56.610 --> 08:59.610
system to reclaim back the memory.

09:01.330 --> 09:08.590
So from this example, we clearly understand that system calls are actually a set of APIs which is invoked

09:08.590 --> 09:15.340
by the application to demand a service from the operating system or kernel.

09:17.360 --> 09:17.900
Right.
