So now I need to do you know write the client side write server side code and so on.
Right.
So let's say at the client side the code that I need to do for creating a socket is provided for me
by the socket module which I need to import.
So the client side code or the server side code is going to be written using the socket model.
So I need to import the socket and there is what I need to do I need to import the socket.
And then I need to use the methods and you know classes of the socket model to go ahead.
So I import the socket module.
So I do that by saying import socket.
Right.
So there is a module called a socket which you can import.
So you import the socket.
Now if you want to use it of course you need to know the methods that are there you need to know how
to create a socket and stuff like that.
So let's go into the documentation.
Let's click on socket right in like purrfect.
So you have one for two and you have one for pipes and three for the pipe and three one right.
So let's say a socket socket you're going for this particular networking protocol out here.
So how do you create a socket A socket is created.
OK so let's say a socket so we come to the screening sockets.
It is the following functions all create socket objects.
So you have mattered call less socket socket got socket.
This particular metor this particular function the following functions are critical.
So there's a function which is helping to create a socket object so socket that socket we're going to
just we're just going to call this function.
Let's create a socket dot suck at this particular function is going to create a socket for us right.
It's going to create it.
These are all going to create socket objects which means if they create a socket object I need to save
that.
So because of the Quine that we got as a glance up though the server socket is also created pretty much
the same itself.
So it doesn't matter until you start doing the other functions yourself socket that socket.
But then of course I need to import socket right.
Suck it up suck it.
This will also return to me a socket which I will store it in servers.
So you have a client socket here you have a server socket here.
Now both of them need to communicate with each other.
Right.
That's our main task to be done.
So let's say the client socket that is with the socket socket I need to communicate.
Right.
OK let's say how will they both talk to each other.
How will the client socket talk to the servers.
OK.
That's our main question.
OK.
So let me say something like this.
What is the next step for us to create the socket.
The next step is Connect the socket to a service.
So I'm going to use the Quine's socket duct have to use a particular method to connect to the service.
So there is a method called connect.
Oh so I need to connect to the server socket I need to give an address of what my server is not yet
active.
So I need to create Sogo socket first.
I need to bind it to a particular IP address and then only I can execute the step of blind socket or
connect.
So I would just I would just hash tag that and I'll go to the servers.
And let me say what can the service socket do first as I need to bind them by and and yeah I need a
binded to give that up all I need to bind it to the IP address of the current computer so that can be
empty.
You don't need to say anything in that but you need to say what is important but that you want to point
to.
Let me say I want to bring it to port number 1 2 3 4 right.
The server socket that bind it bound to a particular port.
One two three four and you have it on the same machine itself.
So the IP address of the machine isn't the same.
So now you will say sure we're going to run the Gwion that we you're going to run the server that you
buy it.
Both are on the same machine.
But you said going in so we're supposed to be a different machines.
Of course that's how it happened in the real world clients and servers are in different machines.
Klein maybe they're on a phone and so maybe they're somewhere else.
But now we are making client server run on the same machine but they are going to run on two different
ports which means people that can really communicate with each other over the network but they're going
to talk to each other.
All we're using different.
There's going to be active at two different porch.
Right.
So even though we technically run them on the same machine OK it's like saying in the same building
they're both running.
OK they both that there.
But the shop is on the ground floor but the client is on the fourth floor.
So the client can any time go down to the shop and talk to what they are both on the same building.
They're both in the same building.
So basically this is like how we're going to say.
So when he says that connect I need to connect to the server and give it in the top.
I want it connected.
I want to connect to the server which isn't the same machine same machine means I need to say local
host.
Which means I want to connect to a server which is on the local host which is on the same machine.
And that was active at port number one to three fourths or connect to the server at port.
One two three four.
Oh yeah.
I've found the server I connect to the client.
All right.
It's got it running but still.
OK.
Now the server has to keep listening for clients.
Listen have to listen you know because there's guys connecting to this guys connecting to the server.
The server has to accept it.
So server socket died die.
Except.
Now what does except return to you what does except return except as a matter which returns two things.
Let's have a look at the documentation.
Oops sorry.
So you have documentation out here and server socket Yes socket objects.
So except it accepts a connection the socket was bound to an address and listing what connections the
return value what does it don't value the return value of accepting a connection the return value is
a pair gone come my address where gone is a new socket object usable to send and receive data on the
connection and addresses the address bound to the socket on the other end of the connection.
So the return is a pair where corn is a new socket object usable to send it on the connection an address
is the address bound to the socket.
On the other end of the connection.
So basically what you're doing is you have a new socket object which you can use to send and receive
data for that particular connection.
OK so the server socket which creates that which the server socket may do and Except you get a new socket
object which is dead and gone.
It is going to be used to talk with that particular plane.
So now if there is another particular line which you accept you will get another call in.
You will get another object a new socket object which can be used for talking to that particular client.
That is too blind to see a plane one plane to each of them are communicated with a new with a completely
different socket object all together and the address is the address of the client which is connected
to the server.
So we are going to accept that into.
Come any.
We can accept that into two variables.
So for now you can print you can print.
Hey I got a client
from address come out easier.
Right.
So before that I can just say print
server is bound to post number two or number one to three for Juergen hardcoded that and now print over
here off the list and print server is listening is listening for Klein's code.
And then when the except happens you accept and then of course it has to keep you know it does now wait
for the client to talk to the server.
Just wait for the client to talk to the server.
So they're going to talk to the server and let's say the server wait until it gets a particular connection
let's say kind died or two.
So we must wait for getting some data.
So let's say receive it takes in some buffer size Let's call this byte number of bytes received that
as data.
Now I want to tell you something about this particular data.
We're going to print that data and we'll understand that when we print data we will understand what
is the kind of data that you get from the client.
Right now the data that you get from the client.
OK the data that you get from the client is going to be bytes.
You can send things on the network as a stream but you have to send bytes one after the other bytes
so bytes needed to be decoded into a string bytes have to be coded into a string.
So you need to say data does decode and you need to use the decode method for decoding it into a string.
So now you have the data that de-code and you can just print that out.
Correct.
Now let's say the client on the other hand is there sending the data selects a client the socket Daut
send all will send all data.
We can just say hi Soval hopes
high server.
You know I am all right.
You can just say I am client I'm a client.
All right.
So that's it for now.
OK.
But then you cannot send it like this because it's you cannot send a string.
You need to send it as bytes so you need to encode them.
Is it right that in code it gets encoded because the default value is UTF 8 which converted into bytes
write down in code and after encoding it after sending everything.
All I need to do is two dots and all.
If let's say the server wants this.
OK.
So let's stop out here let's close the connection let's close the socket so I do client socket dot dot
toot's.
Right now at the same time the server socket also needs to be closed so after that the data that de-code
first is I need to close the active connection.
That's what we had done in point number six close active connections.
I will take kind of close.
And then I will say server underscore socket does close.
Right.