1 00:00:00,790 --> 00:00:08,860 In this lesson, we're going to make a couple of changes to our code, to be able to count the clients. Right 2 00:00:08,890 --> 00:00:09,370 now, 3 00:00:09,400 --> 00:00:22,960 every time we run a client - it just says "Client has connected." 4 00:00:23,030 --> 00:00:27,210 So this is what we see at the server. 5 00:00:27,290 --> 00:00:29,390 If I run another client, 6 00:00:32,420 --> 00:00:35,690 it only says "Client has connected." 7 00:00:35,900 --> 00:00:44,210 I want it to say "Client 1 has connected", "Client 2 has connected"... and then "Client 1 has disconnected", "Client 2 8 00:00:44,240 --> 00:00:47,530 has disconnected", etc. etc. 9 00:00:47,540 --> 00:00:56,420 Now the reason why this isn't as simple as it may sound, is that you may just think - okay we're going 10 00:00:56,420 --> 00:01:05,840 to add a counter to ServerThread for example, because this is where we print out "Client has connected". 11 00:01:08,420 --> 00:01:16,400 Well, it doesn't actually work that way - because every time a new client connects, we open a new server 12 00:01:16,400 --> 00:01:25,290 thread, which means that all of this code is run from the very beginning, for every new client. 13 00:01:25,400 --> 00:01:33,230 That wouldn't work for us because if we were to define a counter here - for example - and say we want to 14 00:01:33,230 --> 00:01:41,450 start counting from one, and then we increment it in this printout for example, every time a new client 15 00:01:41,450 --> 00:01:45,560 connects, we would again start counting from 1. 16 00:01:45,590 --> 00:01:47,680 So that's not going to work. 17 00:01:47,690 --> 00:01:54,040 So the solution here is to count the new clients in the ServerMain class. 18 00:01:54,080 --> 00:02:03,070 I'm going to delete this line since we won't need it, and then I'm going to stop all the clients and the 19 00:02:03,070 --> 00:02:07,450 server that we're running, and I'm going to go to ServerMain. 20 00:02:07,480 --> 00:02:10,840 Let's save all the changes we made. 21 00:02:10,870 --> 00:02:14,500 All right. So we need some kind of a counter. 22 00:02:14,740 --> 00:02:22,720 I'm going to call it client_number for example, and that's going to be a private int. 23 00:02:29,340 --> 00:02:36,540 Then, I'm going to create a method, and that method needs to be public since I'm going to have to access 24 00:02:36,540 --> 00:02:38,050 it from a different class. 25 00:02:38,220 --> 00:02:40,280 It's going to return an integer. 26 00:02:40,290 --> 00:02:44,190 So it's going to be public Integer getClientNumber(). 27 00:02:47,160 --> 00:02:48,540 So it's going to be public Integer getClientNumber(). 28 00:02:51,560 --> 00:02:56,750 And then whenever I call this method, I want it to return 29 00:02:58,990 --> 00:03:08,880 client_number incremented. Now in order to later be able to access this method from our server thread 30 00:03:08,890 --> 00:03:17,650 class, we're going to have to use a ServerMain object. And the type of that object is going to be 31 00:03:17,860 --> 00:03:18,490 ServerMain. 32 00:03:18,520 --> 00:03:26,180 So I have to, kind of, forward this ServerMain class to our ServerThread constructor. 33 00:03:26,260 --> 00:03:33,290 So you're just going to have to bear with me and hopefully it will clear up as we move forward. 34 00:03:33,310 --> 00:03:41,280 So it's actually enough to type this in >>.this<< pretty much forwards everything that we have in this class. 35 00:03:41,310 --> 00:03:46,710 So we're going to be able to use this method from the ServerThread class. 36 00:03:46,930 --> 00:03:49,970 Let's save this and go to ServerThread. 37 00:03:50,020 --> 00:03:53,500 We're going to need the ServerMain object - 38 00:03:58,570 --> 00:04:00,580 its type is ServerMain, 39 00:04:04,340 --> 00:04:06,580 and we've forwarded 40 00:04:06,740 --> 00:04:19,000 this in here, so we need to change the constructor - it's going to say ServerMain server_main, and then 41 00:04:19,000 --> 00:04:20,730 we need to initialize it here. 42 00:04:25,870 --> 00:04:26,850 All right. 43 00:04:26,860 --> 00:04:32,380 Now all we need to do is get the number from server_main. 44 00:04:32,380 --> 00:04:35,350 So I'm going to create a new number. 45 00:04:35,530 --> 00:04:37,360 I'm gonna say client_number .. 46 00:04:41,820 --> 00:04:49,600 client_number = server_main.getClientNumber(). Now let's use that in the printout: 47 00:04:51,940 --> 00:05:03,230 "Client " + client_number, and then I'm going to use a dot + " has connected." 48 00:05:03,230 --> 00:05:11,000 Let's see if we can add a number to this last message instead of "Socket is closed." 49 00:05:11,000 --> 00:05:16,560 I'm going to say "Client" and then 50 00:05:20,260 --> 00:05:24,130 client_number + ". has disconnected." 51 00:05:28,020 --> 00:05:30,960 Let's save this and see if it works. 52 00:05:32,150 --> 00:05:39,490 I'm going to run ServerMain, and then I'm going to run the first client. 53 00:05:39,590 --> 00:05:39,950 OK. 54 00:05:39,950 --> 00:05:48,410 "Client 1 has connected", "Client 2 has connected", "Client 3 has connected." 55 00:05:48,410 --> 00:05:54,460 Let's switch to the clients. I'm just going to say "A" - "Client 1 has disconnected." 56 00:05:54,560 --> 00:05:58,010 Let's go to client 3 for example. 57 00:05:58,010 --> 00:05:59,550 This should be client 3. 58 00:05:59,960 --> 00:06:04,340 I'm going to say "B" - "Client 3 has disconnected." 59 00:06:04,340 --> 00:06:06,490 Let's go to the second client. 60 00:06:06,500 --> 00:06:07,850 That should be this - 61 00:06:10,990 --> 00:06:11,380 ok, 62 00:06:11,380 --> 00:06:14,230 seems that everything is working properly. 63 00:06:14,230 --> 00:06:23,330 I'm going to stop the server, and add something to the client as well, so that the client knows what 64 00:06:23,450 --> 00:06:25,520 is his, or hers, number. 65 00:06:25,530 --> 00:06:28,180 We're going to say it here, 66 00:06:28,180 --> 00:06:38,660 we're going to say "Welcome! You are client number " and then we're going to say client_number - 67 00:06:41,830 --> 00:06:50,280 + "." - you can now run this to see if it works and hopefully continue to play with it. 68 00:06:50,340 --> 00:06:55,470 As always, I'm going to save the code to the lesson so that's where you can find it.