As promised, in this lesson we're going to make our simple little program a bit more interesting. For
the sake of keeping our previous code,
I'm going to create a new packet.
I'm going to right click on the source packet and go to New -> Package.
I'm going to name it simple_tcp_2 for example, and then I'm going to copy the previous two classes by
selecting them, right click -> Copy, and then right click on the simple_tcp_2 package, and then Paste. I'm
going to close the previous two classes and open the two new ones.
The first thing we're going to change is - we're going to enable the client to be able to send something
custom to the server instead of just saying "Thanks". We're going to need a Scanner for this. I'm going
to create a new Scanner
right here. It's going to be Scanner -
for example, "keyboard" - new scanner - and then we're going to use System input.
Then I'm just gonna press Ctrl + Shift +O to import all the necessary packages. And now we need to prompt
the user to type something in. I'm going to say System.out, but I'm not going to use "println", I'm just
going to use "print".
The difference is that this one doesn't add a new line after the text. So the prompt is going to say
"Say something to the server: " for example, and then the user is now supposed to type something in.
So I'm going to use the message String.
All right.
That should be it.
So we're here we're prompting the user to say something - to type something in - to be sent to the server,
and then whatever they type in, it is going to be stored in the message String and then that's what we're
going to send out instead of "Thanks!"
It's just going to be "message".
All right, let's save this and then run it to see if it works.
I'm going to go to the server and I'm going to run the server. And then I'm going to run the client.
And here we can see the prompt.
So I'm going to type in - instead of "Thanks" - let's just say "Ok, whatever"
I'm going to press Enter. And on the server side - in the server console - we can see that this is the message
that we've received.
And then the socket is closed.
That was pretty simple.
I'm going to clean this up and now we're going to complicate things a bit further.
What we're going to do now is - we're going to play a simple game. We'll generate a random number between
1 and 10 on the server, and then we're going to ask the client to guess the number.
First, I'm going to clean up a little bit in the Client class.
So we're going to need this string,
and I'm not actually going to need any of this.
So this is where our new code is going to be at,
and then I'm going to go to the server. Over here we're also going to need the string - or are we? - We're
not, we're actually going to need an integer here. So I'm just going to delete all of this
and I'm going to start by generating a random number.
It's going to be an integer and I'm going to call it secret_number.
Now this is just one of the ways you can do this.
I'm going to use the Random() method.
What this means is - give me a random number out of the first 10 numbers - but always remember that we as
engineers, and especially if you're a programmer - you always count from zero.
So this would actually return a random number between 0 and 9.
So in order to get a number between 1 and 10, we're just going to add 1 to whatever this returns. Next,
we're going to use a do-while loop.
If you are a total beginner to programming, just a short explanation here: a do-while loop lets you run
through the loop at least once.
That means we only check the condition to go into the loop at the end of the first run.
So you actually have to go through the code at least once to get to the condition.
I'm going to say do -
and then we're going to prompt the user to guess a number right away.
So we're going to use the out_socket for that. out_socket.println - and then we're going to send "Guess
"Guess a number from 1 to 10: "
Next,
we're expecting a message back from the client.
OK.
So it turns out I am going to need that string after all - so the message is going to come through the
socket.
It's going to be in_socket.readLine()
in_socket.readLine()
and now let me define that string.
All right.
So what we have here is the first time we run into this code - into this loop
in particular - we are going to send a message to the client saying "Guess a number from 1 to 10", and then
we're expecting a message back from the client, where in this message we have a number.
So what we're going to have to do is - we're going to have to translate that message - which is a string -
OK as long as we use this readLine() method,
we're going to be getting a string.
Now this is a number, so I have to translate that number from a string to an integer.
I'll do this within the while condition.
So I'm going to say - all right -
Now bear with me: integer.parseInt(message) - and we're going to compare that to secret_number, but we need
that to be not equal to our secret_number.
Let me try and explain this a bit better.
So, as long as this condition is met, and this is pretty much saying: "OK so this is what the client is
giving us, and as long as they're not hitting the right number, we need to go back into the loop."
We need to say "Guess a number"
again. That means that they haven't got the right number yet.
So we keep sending them the prompt and the opportunity to guess the number once more. For the first time
when they hit the right number -
- so when they type in the exact number that we have generated here - we're going to get out of this loop.
When we do get out of the loop,
we're going to send out_socket.println("You got it!!!") - for example. You can send whatever you want.
And then I'm going to print out in the servers console "Secret number is out." - for example - "Exiting the
app." - that's hopefully it for the server,
I'm going to save this and switch to the client. The client is going to be guessing a number. I'm going
to call that "user_number", and we're going to be creating a loop here too.
It's going to be a regular while loop, and the condition is going to be: read from the in_socket,
in_socket.readln, and then if that line starts with "Guess",
that means we haven't guessed the number yet, and we're getting into the loop. As long as we're in the
loop,
We're going to be printing out a prompt saying - for example, "Servers says:
Guess a number from 1 to 10."
and then we're going to prompt our user to type in a number.
We're going to store whatever they type in into our user_number string.
And we're going to simply just send out that string to the server.
We're going to use the out_socket for that.
We're just going to say println(user_number).
As long as we send the wrong number,
we will be getting a message back, saying "Guess a number."
So this condition will be met as long as we're hitting the wrong number, and we will keep going through
this loop.
So as long as we're getting a sentence beginning with a "Guess", we need to be in this loop. As soon as
we get something else,
the other thing we can get is "You got it!!!" -
- we are going to fall out of our loop.
So we're just going to print out when that happens. Obviously, right after this bracket, we're out of the
loop and we can print out:
"You got it!!!". Let's save this and see if it works.
As always, I'm going to run the server first, and then I'm going to run the client.
Ok, let's play the game. So the server is saying - you need to make sure that you're in the client console
window - so the server is saying "Guess a number." - I'm going to try 1, I haven't got it yet, so I'm going to just
go from 1 to 10, I'm going to 2,
3, 4, 5, 6 - and I got it!
It's 6. Of course, as soon as you get the right number the next line of code for both the client and the
server is going to be socket.close(). So the socket is going to be closed right after we get this message
that we have hit the right number.
Now this was very basic but hopefully it got you interested to continue to play around with the code.
If you have any questions or issues send me a message or just post it in the Q&A section!