WEBVTT

00:00.380 --> 00:07.970
So now I need to do you know write the client side write server side code and so on.

00:07.980 --> 00:08.290
Right.

00:08.320 --> 00:17.160
So let's say at the client side the code that I need to do for creating a socket is provided for me

00:17.610 --> 00:19.850
by the socket module which I need to import.

00:19.860 --> 00:24.900
So the client side code or the server side code is going to be written using the socket model.

00:24.930 --> 00:31.040
So I need to import the socket and there is what I need to do I need to import the socket.

00:31.410 --> 00:38.780
And then I need to use the methods and you know classes of the socket model to go ahead.

00:39.000 --> 00:40.790
So I import the socket module.

00:41.040 --> 00:43.740
So I do that by saying import socket.

00:43.920 --> 00:44.330
Right.

00:44.400 --> 00:48.570
So there is a module called a socket which you can import.

00:48.780 --> 00:50.150
So you import the socket.

00:50.530 --> 00:56.130
Now if you want to use it of course you need to know the methods that are there you need to know how

00:56.130 --> 00:59.160
to create a socket and stuff like that.

00:59.160 --> 01:01.560
So let's go into the documentation.

01:01.560 --> 01:08.270
Let's click on socket right in like purrfect.

01:08.350 --> 01:15.140
So you have one for two and you have one for pipes and three for the pipe and three one right.

01:15.370 --> 01:22.330
So let's say a socket socket you're going for this particular networking protocol out here.

01:22.330 --> 01:26.680
So how do you create a socket A socket is created.

01:26.890 --> 01:31.150
OK so let's say a socket so we come to the screening sockets.

01:31.180 --> 01:35.250
It is the following functions all create socket objects.

01:35.350 --> 01:40.690
So you have mattered call less socket socket got socket.

01:40.690 --> 01:44.920
This particular metor this particular function the following functions are critical.

01:44.920 --> 01:50.350
So there's a function which is helping to create a socket object so socket that socket we're going to

01:50.350 --> 01:52.910
just we're just going to call this function.

01:53.290 --> 02:01.990
Let's create a socket dot suck at this particular function is going to create a socket for us right.

02:01.990 --> 02:02.970
It's going to create it.

02:03.020 --> 02:07.250
These are all going to create socket objects which means if they create a socket object I need to save

02:07.260 --> 02:07.930
that.

02:07.980 --> 02:15.010
So because of the Quine that we got as a glance up though the server socket is also created pretty much

02:15.010 --> 02:16.170
the same itself.

02:16.180 --> 02:22.700
So it doesn't matter until you start doing the other functions yourself socket that socket.

02:22.900 --> 02:26.100
But then of course I need to import socket right.

02:26.110 --> 02:27.790
Suck it up suck it.

02:27.790 --> 02:32.760
This will also return to me a socket which I will store it in servers.

02:33.800 --> 02:37.560
So you have a client socket here you have a server socket here.

02:37.580 --> 02:39.780
Now both of them need to communicate with each other.

02:39.800 --> 02:40.370
Right.

02:40.460 --> 02:42.460
That's our main task to be done.

02:42.680 --> 02:47.390
So let's say the client socket that is with the socket socket I need to communicate.

02:47.390 --> 02:47.950
Right.

02:48.210 --> 02:52.270
OK let's say how will they both talk to each other.

02:52.310 --> 02:55.220
How will the client socket talk to the servers.

02:55.410 --> 02:55.820
OK.

02:55.850 --> 02:57.990
That's our main question.

02:58.100 --> 02:58.500
OK.

02:58.610 --> 03:01.030
So let me say something like this.

03:01.280 --> 03:04.120
What is the next step for us to create the socket.

03:04.130 --> 03:06.790
The next step is Connect the socket to a service.

03:06.980 --> 03:14.160
So I'm going to use the Quine's socket duct have to use a particular method to connect to the service.

03:14.390 --> 03:17.050
So there is a method called connect.

03:17.430 --> 03:22.330
Oh so I need to connect to the server socket I need to give an address of what my server is not yet

03:22.330 --> 03:23.380
active.

03:23.390 --> 03:27.620
So I need to create Sogo socket first.

03:27.650 --> 03:33.730
I need to bind it to a particular IP address and then only I can execute the step of blind socket or

03:33.740 --> 03:34.040
connect.

03:34.040 --> 03:40.020
So I would just I would just hash tag that and I'll go to the servers.

03:40.160 --> 03:46.550
And let me say what can the service socket do first as I need to bind them by and and yeah I need a

03:46.630 --> 03:54.860
binded to give that up all I need to bind it to the IP address of the current computer so that can be

03:54.860 --> 03:55.450
empty.

03:55.460 --> 03:59.960
You don't need to say anything in that but you need to say what is important but that you want to point

03:59.970 --> 04:00.240
to.

04:00.260 --> 04:03.860
Let me say I want to bring it to port number 1 2 3 4 right.

04:03.960 --> 04:07.160
The server socket that bind it bound to a particular port.

04:07.160 --> 04:11.540
One two three four and you have it on the same machine itself.

04:11.540 --> 04:14.350
So the IP address of the machine isn't the same.

04:14.510 --> 04:18.710
So now you will say sure we're going to run the Gwion that we you're going to run the server that you

04:18.710 --> 04:19.030
buy it.

04:19.030 --> 04:20.060
Both are on the same machine.

04:20.060 --> 04:23.110
But you said going in so we're supposed to be a different machines.

04:23.180 --> 04:29.070
Of course that's how it happened in the real world clients and servers are in different machines.

04:29.080 --> 04:32.660
Klein maybe they're on a phone and so maybe they're somewhere else.

04:32.900 --> 04:38.090
But now we are making client server run on the same machine but they are going to run on two different

04:38.090 --> 04:43.130
ports which means people that can really communicate with each other over the network but they're going

04:43.130 --> 04:44.460
to talk to each other.

04:44.540 --> 04:46.800
All we're using different.

04:46.830 --> 04:49.940
There's going to be active at two different porch.

04:50.090 --> 04:50.810
Right.

04:50.810 --> 04:56.870
So even though we technically run them on the same machine OK it's like saying in the same building

04:56.930 --> 04:58.280
they're both running.

04:58.490 --> 04:59.430
OK they both that there.

04:59.450 --> 05:04.220
But the shop is on the ground floor but the client is on the fourth floor.

05:04.310 --> 05:10.280
So the client can any time go down to the shop and talk to what they are both on the same building.

05:10.300 --> 05:12.390
They're both in the same building.

05:12.410 --> 05:15.730
So basically this is like how we're going to say.

05:16.070 --> 05:21.810
So when he says that connect I need to connect to the server and give it in the top.

05:22.610 --> 05:23.790
I want it connected.

05:23.790 --> 05:29.660
I want to connect to the server which isn't the same machine same machine means I need to say local

05:29.660 --> 05:30.230
host.

05:30.440 --> 05:35.440
Which means I want to connect to a server which is on the local host which is on the same machine.

05:35.690 --> 05:39.960
And that was active at port number one to three fourths or connect to the server at port.

05:40.000 --> 05:40.860
One two three four.

05:40.910 --> 05:41.500
Oh yeah.

05:41.510 --> 05:45.040
I've found the server I connect to the client.

05:45.080 --> 05:45.650
All right.

05:45.710 --> 05:47.550
It's got it running but still.

05:47.690 --> 05:48.210
OK.

05:48.350 --> 05:50.870
Now the server has to keep listening for clients.

05:51.140 --> 05:59.260
Listen have to listen you know because there's guys connecting to this guys connecting to the server.

05:59.280 --> 06:01.000
The server has to accept it.

06:01.050 --> 06:04.060
So server socket died die.

06:04.110 --> 06:04.900
Except.

06:05.190 --> 06:11.850
Now what does except return to you what does except return except as a matter which returns two things.

06:11.850 --> 06:13.530
Let's have a look at the documentation.

06:14.650 --> 06:18.600
Oops sorry.

06:18.870 --> 06:26.480
So you have documentation out here and server socket Yes socket objects.

06:26.500 --> 06:31.080
So except it accepts a connection the socket was bound to an address and listing what connections the

06:31.080 --> 06:37.820
return value what does it don't value the return value of accepting a connection the return value is

06:37.810 --> 06:46.960
a pair gone come my address where gone is a new socket object usable to send and receive data on the

06:46.960 --> 06:53.880
connection and addresses the address bound to the socket on the other end of the connection.

06:53.920 --> 07:04.030
So the return is a pair where corn is a new socket object usable to send it on the connection an address

07:04.030 --> 07:06.410
is the address bound to the socket.

07:06.430 --> 07:07.660
On the other end of the connection.

07:07.660 --> 07:13.720
So basically what you're doing is you have a new socket object which you can use to send and receive

07:13.720 --> 07:17.980
data for that particular connection.

07:18.160 --> 07:25.750
OK so the server socket which creates that which the server socket may do and Except you get a new socket

07:25.750 --> 07:29.020
object which is dead and gone.

07:29.130 --> 07:33.220
It is going to be used to talk with that particular plane.

07:33.220 --> 07:37.250
So now if there is another particular line which you accept you will get another call in.

07:37.300 --> 07:43.200
You will get another object a new socket object which can be used for talking to that particular client.

07:43.210 --> 07:49.540
That is too blind to see a plane one plane to each of them are communicated with a new with a completely

07:49.540 --> 07:55.210
different socket object all together and the address is the address of the client which is connected

07:55.210 --> 07:56.420
to the server.

07:56.770 --> 08:00.850
So we are going to accept that into.

08:01.070 --> 08:02.380
Come any.

08:02.520 --> 08:05.730
We can accept that into two variables.

08:05.740 --> 08:09.140
So for now you can print you can print.

08:09.550 --> 08:11.260
Hey I got a client

08:13.600 --> 08:18.820
from address come out easier.

08:19.030 --> 08:19.600
Right.

08:19.830 --> 08:23.710
So before that I can just say print

08:27.010 --> 08:40.500
server is bound to post number two or number one to three for Juergen hardcoded that and now print over

08:40.510 --> 08:50.960
here off the list and print server is listening is listening for Klein's code.

08:51.070 --> 08:59.290
And then when the except happens you accept and then of course it has to keep you know it does now wait

08:59.290 --> 09:02.430
for the client to talk to the server.

09:02.570 --> 09:04.890
Just wait for the client to talk to the server.

09:04.920 --> 09:11.250
So they're going to talk to the server and let's say the server wait until it gets a particular connection

09:11.260 --> 09:14.410
let's say kind died or two.

09:14.530 --> 09:16.810
So we must wait for getting some data.

09:16.900 --> 09:24.970
So let's say receive it takes in some buffer size Let's call this byte number of bytes received that

09:25.300 --> 09:26.310
as data.

09:26.320 --> 09:29.040
Now I want to tell you something about this particular data.

09:29.120 --> 09:33.340
We're going to print that data and we'll understand that when we print data we will understand what

09:33.340 --> 09:35.970
is the kind of data that you get from the client.

09:36.100 --> 09:39.710
Right now the data that you get from the client.

09:39.820 --> 09:43.030
OK the data that you get from the client is going to be bytes.

09:43.030 --> 09:48.370
You can send things on the network as a stream but you have to send bytes one after the other bytes

09:48.720 --> 09:54.220
so bytes needed to be decoded into a string bytes have to be coded into a string.

09:54.220 --> 10:00.920
So you need to say data does decode and you need to use the decode method for decoding it into a string.

10:01.150 --> 10:06.100
So now you have the data that de-code and you can just print that out.

10:06.100 --> 10:07.010
Correct.

10:07.010 --> 10:15.240
Now let's say the client on the other hand is there sending the data selects a client the socket Daut

10:15.950 --> 10:18.870
send all will send all data.

10:19.100 --> 10:24.240
We can just say hi Soval hopes

10:26.890 --> 10:28.450
high server.

10:28.790 --> 10:33.020
You know I am all right.

10:33.200 --> 10:37.850
You can just say I am client I'm a client.

10:37.850 --> 10:38.410
All right.

10:38.480 --> 10:40.830
So that's it for now.

10:41.050 --> 10:41.470
OK.

10:41.510 --> 10:44.930
But then you cannot send it like this because it's you cannot send a string.

10:44.930 --> 10:48.710
You need to send it as bytes so you need to encode them.

10:48.830 --> 10:56.150
Is it right that in code it gets encoded because the default value is UTF 8 which converted into bytes

10:56.550 --> 11:01.620
write down in code and after encoding it after sending everything.

11:01.640 --> 11:04.360
All I need to do is two dots and all.

11:04.720 --> 11:07.240
If let's say the server wants this.

11:07.280 --> 11:07.630
OK.

11:07.670 --> 11:14.960
So let's stop out here let's close the connection let's close the socket so I do client socket dot dot

11:15.060 --> 11:17.710
toot's.

11:17.810 --> 11:26.030
Right now at the same time the server socket also needs to be closed so after that the data that de-code

11:26.030 --> 11:28.820
first is I need to close the active connection.

11:28.820 --> 11:32.080
That's what we had done in point number six close active connections.

11:32.090 --> 11:34.710
I will take kind of close.

11:35.030 --> 11:40.040
And then I will say server underscore socket does close.

11:40.380 --> 11:40.910
Right.
