1 00:00:00,060 --> 00:00:03,640 Hello, everyone, welcome to another video of our Pathum lessons. 2 00:00:04,700 --> 00:00:10,770 Last time we talked about Tsipi sockets, but in general there's a little issue with sockets if we want 3 00:00:10,770 --> 00:00:13,070 to deal with many clients at the same time. 4 00:00:14,490 --> 00:00:20,520 Before we continue, let's try first connect to our server from multiple clients and see what happens. 5 00:01:07,770 --> 00:01:15,090 As you see here, we can only send one time since our wire loop starts with that with except in connection 6 00:01:15,090 --> 00:01:17,040 first from the new client. 7 00:01:20,750 --> 00:01:27,560 After that, they will receive the data, so once it receives a data one time from the client, it will 8 00:01:27,560 --> 00:01:30,430 go up again to wait for a new client to connect. 9 00:01:31,490 --> 00:01:37,310 And if we move our receive function in another way loop, then how do we still accept connections from 10 00:01:37,310 --> 00:01:42,290 the new clients if we are stuck with one inner while loop with one client? 11 00:01:43,610 --> 00:01:44,810 Let me show you what I mean. 12 00:01:47,380 --> 00:01:51,820 Let's move our embassy function, this one in another wire loop. 13 00:01:54,810 --> 00:01:56,820 So we can keep receiving data. 14 00:02:21,220 --> 00:02:24,430 Now, let's try to connect again from one client first. 15 00:02:33,380 --> 00:02:36,370 OK, what if we connect from Windows now? 16 00:02:39,070 --> 00:02:47,740 We can't there's nothing happened was that because we are stuck in one wire loop and we can break out 17 00:02:47,740 --> 00:02:51,940 of the loop so we can receive another connection from Windows? 18 00:02:54,490 --> 00:02:56,110 Let's look at the code again. 19 00:02:59,560 --> 00:03:03,080 So we are stuck in this wild loop and we can't exit this way. 20 00:03:04,570 --> 00:03:05,740 We have to wait. 21 00:03:06,100 --> 00:03:09,160 Our client, the first one to break the connection. 22 00:03:10,630 --> 00:03:15,400 Let's add a brake line here if the connection was broken by the client. 23 00:03:32,160 --> 00:03:35,370 Now, let's break the connection using control policy. 24 00:03:38,190 --> 00:03:45,360 Now, the while loop we are using, the inner while loop is broken now and we went back to the accept 25 00:03:45,360 --> 00:03:45,850 function. 26 00:03:46,740 --> 00:03:48,960 So now this client can connect. 27 00:03:50,870 --> 00:03:52,640 But that's not what we want, right? 28 00:03:54,140 --> 00:03:59,120 That's why we need multithreaded socket so we can receive from multiple clients at the same time. 29 00:03:59,780 --> 00:04:02,990 Threat is basically running independently in the background. 30 00:04:03,080 --> 00:04:07,690 So every time we receive a connection from a client, it would run in the background. 31 00:04:07,700 --> 00:04:11,390 So our program will be ready to receive another one and handle it. 32 00:04:12,530 --> 00:04:16,490 Let's create our Thrillist function now so we can call it whenever we want. 33 00:04:18,440 --> 00:04:19,520 It looks like this. 34 00:04:22,460 --> 00:04:24,650 And of course, you can call it anything you want. 35 00:05:10,480 --> 00:05:17,110 This function takes only one argument, which is the socket object we created before and stored in the 36 00:05:17,110 --> 00:05:23,870 C variable, then inside the function we will receive data in a variable called data. 37 00:05:24,310 --> 00:05:25,210 Then we print it. 38 00:05:26,800 --> 00:05:29,650 Now let's see how we open this function in a new thread. 39 00:05:34,830 --> 00:05:38,040 We can do that by using the start new thread function. 40 00:05:39,060 --> 00:05:45,420 This function takes two arguments, the first argument is the function we want to open in a new thread, 41 00:05:45,420 --> 00:05:47,550 which in our case was called threated. 42 00:05:49,290 --> 00:05:50,520 The second argument. 43 00:05:52,440 --> 00:05:53,940 Is our socket object. 44 00:05:55,220 --> 00:05:59,870 Keep in mind that we need to add a comma if if it takes only one argument. 45 00:06:03,500 --> 00:06:05,060 Let's add that to our program. 46 00:06:35,280 --> 00:06:40,350 I forgot to mention that we need to import our thread module at the top of our program. 47 00:06:44,340 --> 00:06:45,270 Now let's run it. 48 00:07:14,940 --> 00:07:21,360 If you noticed, we can always send one time, but also we want to keep receiving from the same client, 49 00:07:22,170 --> 00:07:25,370 so we need to put our resume function in a while loop. 50 00:07:26,280 --> 00:07:27,060 Let's add the. 51 00:07:56,280 --> 00:08:00,270 We need also to check if the data was broken with our client. 52 00:08:06,760 --> 00:08:12,400 You can do that by checking if there is equal, the length of data is equal to zero or just we can say, 53 00:08:12,400 --> 00:08:13,630 if not data. 54 00:08:30,130 --> 00:08:31,150 And then we break it. 55 00:08:35,390 --> 00:08:38,300 Nothing happened, we just print our data. 56 00:08:44,480 --> 00:08:45,290 Let's bring in. 57 00:09:04,440 --> 00:09:13,410 If you noticed, our clients are both stuck, we can get to send some anything else, that's because 58 00:09:13,800 --> 00:09:19,490 my client is waiting to receive some data from the server, but the server is not sending anything. 59 00:09:20,280 --> 00:09:26,640 So they will keep stuck on this mode like the receiving mode until the server is sending something back. 60 00:09:27,250 --> 00:09:28,130 Let's fix that. 61 00:09:32,250 --> 00:09:38,220 So we come here after we print our data, we can also send something back to our client. 62 00:09:46,250 --> 00:09:49,100 We can send the same data that we received like this. 63 00:09:51,530 --> 00:09:52,660 That's all we need to do. 64 00:10:17,690 --> 00:10:19,610 So, you know, we can keep sending commands. 65 00:10:24,480 --> 00:10:25,740 Let's try our Mac 66 00:10:31,290 --> 00:10:39,900 nice also about this line here to reuse the same socket when we close and reopen our Python program, 67 00:10:40,380 --> 00:10:46,980 because sometimes when you close the Python program and you reopen it again, the same socket is not 68 00:10:46,980 --> 00:10:47,400 used. 69 00:10:48,030 --> 00:10:52,440 It will it will tell you an error that the socket is used by another program. 70 00:10:52,740 --> 00:10:53,720 But actually it's not. 71 00:10:54,540 --> 00:10:59,400 So by adding this line, you make sure you use the same socket by the same program. 72 00:10:59,400 --> 00:11:00,690 If you close them, reopen it. 73 00:11:02,940 --> 00:11:04,720 We have reached the end of this lesson. 74 00:11:04,740 --> 00:11:05,400 Thank you. 75 00:11:05,400 --> 00:11:06,540 And see you in the next one.