In this lesson, we're going to make a couple of changes to our code, to be able to count the clients. Right
now,
every time we run a client - it just says "Client has connected."
So this is what we see at the server.
If I run another client,
it only says "Client has connected."
I want it to say "Client 1 has connected", "Client 2 has connected"... and then "Client 1 has disconnected", "Client 2
has disconnected", etc. etc.
Now the reason why this isn't as simple as it may sound, is that you may just think - okay we're going
to add a counter to ServerThread for example, because this is where we print out "Client has connected".
Well, it doesn't actually work that way - because every time a new client connects, we open a new server
thread, which means that all of this code is run from the very beginning, for every new client.
That wouldn't work for us because if we were to define a counter here - for example - and say we want to
start counting from one, and then we increment it in this printout for example, every time a new client
connects, we would again start counting from 1.
So that's not going to work.
So the solution here is to count the new clients in the ServerMain class.
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
server that we're running, and I'm going to go to ServerMain.
Let's save all the changes we made.
All right. So we need some kind of a counter.
I'm going to call it client_number for example, and that's going to be a private int.
Then, I'm going to create a method, and that method needs to be public since I'm going to have to access
it from a different class.
It's going to return an integer.
So it's going to be public Integer getClientNumber().
And then whenever I call this method, I want it to return
client_number incremented. Now in order to later be able to access this method from our server thread
class, we're going to have to use a ServerMain object. And the type of that object is going to be
ServerMain.
So I have to, kind of, forward this ServerMain class to our ServerThread constructor.
So you're just going to have to bear with me and hopefully it will clear up as we move forward.
So it's actually enough to type this in >>.this<< pretty much forwards everything that we have in this class.
So we're going to be able to use this method from the ServerThread class.
Let's save this and go to ServerThread.
We're going to need the ServerMain object -
its type is ServerMain,
and we've forwarded
this in here, so we need to change the constructor - it's going to say ServerMain server_main, and then
we need to initialize it here.
All right.
Now all we need to do is get the number from server_main.
So I'm going to create a new number.
I'm gonna say client_number ..
client_number = server_main.getClientNumber(). Now let's use that in the printout:
"Client " + client_number, and then I'm going to use a dot + " has connected."
Let's see if we can add a number to this last message instead of "Socket is closed."
I'm going to say "Client" and then
client_number + ". has disconnected."
Let's save this and see if it works.
I'm going to run ServerMain, and then I'm going to run the first client.
OK.
"Client 1 has connected", "Client 2 has connected", "Client 3 has connected."
Let's switch to the clients. I'm just going to say "A" - "Client 1 has disconnected."
Let's go to client 3 for example.
This should be client 3.
I'm going to say "B" - "Client 3 has disconnected."
Let's go to the second client.
That should be this -
ok,
seems that everything is working properly.
I'm going to stop the server, and add something to the client as well, so that the client knows what
is his, or hers, number.
We're going to say it here,
we're going to say "Welcome! You are client number " and then we're going to say client_number -
+ "." - you can now run this to see if it works and hopefully continue to play with it.
As always, I'm going to save the code to the lesson so that's where you can find it.