Hello, everyone, welcome to another video of our Pathum lessons.
Last time we talked about Tsipi sockets, but in general there's a little issue with sockets if we want
to deal with many clients at the same time.
Before we continue, let's try first connect to our server from multiple clients and see what happens.
As you see here, we can only send one time since our wire loop starts with that with except in connection
first from the new client.
After that, they will receive the data, so once it receives a data one time from the client, it will
go up again to wait for a new client to connect.
And if we move our receive function in another way loop, then how do we still accept connections from
the new clients if we are stuck with one inner while loop with one client?
Let me show you what I mean.
Let's move our embassy function, this one in another wire loop.
So we can keep receiving data.
Now, let's try to connect again from one client first.
OK, what if we connect from Windows now?
We can't there's nothing happened was that because we are stuck in one wire loop and we can break out
of the loop so we can receive another connection from Windows?
Let's look at the code again.
So we are stuck in this wild loop and we can't exit this way.
We have to wait.
Our client, the first one to break the connection.
Let's add a brake line here if the connection was broken by the client.
Now, let's break the connection using control policy.
Now, the while loop we are using, the inner while loop is broken now and we went back to the accept
function.
So now this client can connect.
But that's not what we want, right?
That's why we need multithreaded socket so we can receive from multiple clients at the same time.
Threat is basically running independently in the background.
So every time we receive a connection from a client, it would run in the background.
So our program will be ready to receive another one and handle it.
Let's create our Thrillist function now so we can call it whenever we want.
It looks like this.
And of course, you can call it anything you want.
This function takes only one argument, which is the socket object we created before and stored in the
C variable, then inside the function we will receive data in a variable called data.
Then we print it.
Now let's see how we open this function in a new thread.
We can do that by using the start new thread function.
This function takes two arguments, the first argument is the function we want to open in a new thread,
which in our case was called threated.
The second argument.
Is our socket object.
Keep in mind that we need to add a comma if if it takes only one argument.
Let's add that to our program.
I forgot to mention that we need to import our thread module at the top of our program.
Now let's run it.
If you noticed, we can always send one time, but also we want to keep receiving from the same client,
so we need to put our resume function in a while loop.
Let's add the.
We need also to check if the data was broken with our client.
You can do that by checking if there is equal, the length of data is equal to zero or just we can say,
if not data.
And then we break it.
Nothing happened, we just print our data.
Let's bring in.
If you noticed, our clients are both stuck, we can get to send some anything else, that's because
my client is waiting to receive some data from the server, but the server is not sending anything.
So they will keep stuck on this mode like the receiving mode until the server is sending something back.
Let's fix that.
So we come here after we print our data, we can also send something back to our client.
We can send the same data that we received like this.
That's all we need to do.
So, you know, we can keep sending commands.
Let's try our Mac
nice also about this line here to reuse the same socket when we close and reopen our Python program,
because sometimes when you close the Python program and you reopen it again, the same socket is not
used.
It will it will tell you an error that the socket is used by another program.
But actually it's not.
So by adding this line, you make sure you use the same socket by the same program.
If you close them, reopen it.
We have reached the end of this lesson.
Thank you.
And see you in the next one.