1 00:00:00,790 --> 00:00:09,160 As promised, in this lesson we're going to make our simple little program a bit more interesting. For 2 00:00:09,160 --> 00:00:13,020 the sake of keeping our previous code, 3 00:00:13,060 --> 00:00:16,130 I'm going to create a new packet. 4 00:00:16,480 --> 00:00:23,140 I'm going to right click on the source packet and go to New -> Package. 5 00:00:23,200 --> 00:00:36,030 I'm going to name it simple_tcp_2 for example, and then I'm going to copy the previous two classes by 6 00:00:36,030 --> 00:00:47,390 selecting them, right click -> Copy, and then right click on the simple_tcp_2 package, and then Paste. I'm 7 00:00:47,390 --> 00:00:57,880 going to close the previous two classes and open the two new ones. 8 00:00:57,910 --> 00:01:04,720 The first thing we're going to change is - we're going to enable the client to be able to send something 9 00:01:04,720 --> 00:01:12,670 custom to the server instead of just saying "Thanks". We're going to need a Scanner for this. I'm going 10 00:01:12,670 --> 00:01:14,910 to create a new Scanner 11 00:01:14,920 --> 00:01:18,890 right here. It's going to be Scanner - 12 00:01:18,910 --> 00:01:28,080 for example, "keyboard" - new scanner - and then we're going to use System input. 13 00:01:33,500 --> 00:01:43,430 Then I'm just gonna press Ctrl + Shift +O to import all the necessary packages. And now we need to prompt 14 00:01:43,460 --> 00:01:51,800 the user to type something in. I'm going to say System.out, but I'm not going to use "println", I'm just 15 00:01:51,800 --> 00:01:55,400 going to use "print". 16 00:01:55,540 --> 00:02:05,800 The difference is that this one doesn't add a new line after the text. So the prompt is going to say 17 00:02:06,190 --> 00:02:17,240 "Say something to the server: " for example, and then the user is now supposed to type something in. 18 00:02:17,240 --> 00:02:20,990 So I'm going to use the message String. 19 00:02:43,170 --> 00:02:43,880 All right. 20 00:02:43,880 --> 00:02:45,140 That should be it. 21 00:02:45,140 --> 00:02:52,520 So we're here we're prompting the user to say something - to type something in - to be sent to the server, 22 00:02:52,940 --> 00:03:00,080 and then whatever they type in, it is going to be stored in the message String and then that's what we're 23 00:03:00,080 --> 00:03:02,950 going to send out instead of "Thanks!" 24 00:03:03,020 --> 00:03:06,490 It's just going to be "message". 25 00:03:06,520 --> 00:03:11,770 All right, let's save this and then run it to see if it works. 26 00:03:11,780 --> 00:03:20,180 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. 27 00:03:23,200 --> 00:03:25,630 And here we can see the prompt. 28 00:03:25,630 --> 00:03:35,920 So I'm going to type in - instead of "Thanks" - let's just say "Ok, whatever" 29 00:03:38,540 --> 00:03:47,420 I'm going to press Enter. And on the server side - in the server console - we can see that this is the message 30 00:03:47,420 --> 00:03:49,310 that we've received. 31 00:03:49,310 --> 00:03:52,410 And then the socket is closed. 32 00:03:52,430 --> 00:03:53,770 That was pretty simple. 33 00:03:53,770 --> 00:04:00,440 I'm going to clean this up and now we're going to complicate things a bit further. 34 00:04:00,440 --> 00:04:08,930 What we're going to do now is - we're going to play a simple game. We'll generate a random number between 35 00:04:08,930 --> 00:04:16,190 1 and 10 on the server, and then we're going to ask the client to guess the number. 36 00:04:16,370 --> 00:04:20,990 First, I'm going to clean up a little bit in the Client class. 37 00:04:21,200 --> 00:04:24,290 So we're going to need this string, 38 00:04:29,230 --> 00:04:34,170 and I'm not actually going to need any of this. 39 00:04:34,210 --> 00:04:39,070 So this is where our new code is going to be at, 40 00:04:42,710 --> 00:04:52,010 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 41 00:04:52,010 --> 00:04:58,940 not, we're actually going to need an integer here. So I'm just going to delete all of this 42 00:05:02,260 --> 00:05:06,030 and I'm going to start by generating a random number. 43 00:05:06,040 --> 00:05:12,500 It's going to be an integer and I'm going to call it secret_number. 44 00:05:12,510 --> 00:05:15,390 Now this is just one of the ways you can do this. 45 00:05:15,390 --> 00:05:19,050 I'm going to use the Random() method. 46 00:05:26,640 --> 00:05:37,200 What this means is - give me a random number out of the first 10 numbers - but always remember that we as 47 00:05:37,200 --> 00:05:43,190 engineers, and especially if you're a programmer - you always count from zero. 48 00:05:43,200 --> 00:05:49,410 So this would actually return a random number between 0 and 9. 49 00:05:49,440 --> 00:06:00,330 So in order to get a number between 1 and 10, we're just going to add 1 to whatever this returns. Next, 50 00:06:00,360 --> 00:06:04,350 we're going to use a do-while loop. 51 00:06:04,350 --> 00:06:14,830 If you are a total beginner to programming, just a short explanation here: a do-while loop lets you run 52 00:06:14,830 --> 00:06:17,580 through the loop at least once. 53 00:06:17,580 --> 00:06:24,840 That means we only check the condition to go into the loop at the end of the first run. 54 00:06:24,870 --> 00:06:31,320 So you actually have to go through the code at least once to get to the condition. 55 00:06:31,440 --> 00:06:33,740 I'm going to say do - 56 00:06:34,440 --> 00:06:39,800 and then we're going to prompt the user to guess a number right away. 57 00:06:39,810 --> 00:06:50,660 So we're going to use the out_socket for that. out_socket.println - and then we're going to send "Guess 58 00:06:50,950 --> 00:06:54,520 "Guess a number from 1 to 10: " 59 00:06:58,030 --> 00:06:58,650 Next, 60 00:06:58,660 --> 00:07:03,800 we're expecting a message back from the client. 61 00:07:03,820 --> 00:07:04,180 OK. 62 00:07:04,180 --> 00:07:10,780 So it turns out I am going to need that string after all - so the message is going to come through the 63 00:07:10,840 --> 00:07:12,310 socket. 64 00:07:12,310 --> 00:07:14,300 It's going to be in_socket.readLine() 65 00:07:14,320 --> 00:07:15,760 in_socket.readLine() 66 00:07:18,660 --> 00:07:25,780 and now let me define that string. 67 00:07:25,830 --> 00:07:26,320 All right. 68 00:07:26,320 --> 00:07:31,150 So what we have here is the first time we run into this code - into this loop 69 00:07:31,150 --> 00:07:38,890 in particular - we are going to send a message to the client saying "Guess a number from 1 to 10", and then 70 00:07:39,070 --> 00:07:45,690 we're expecting a message back from the client, where in this message we have a number. 71 00:07:45,700 --> 00:07:51,360 So what we're going to have to do is - we're going to have to translate that message - which is a string - 72 00:07:51,790 --> 00:07:55,750 OK as long as we use this readLine() method, 73 00:07:55,840 --> 00:07:58,250 we're going to be getting a string. 74 00:07:58,360 --> 00:08:05,830 Now this is a number, so I have to translate that number from a string to an integer. 75 00:08:05,830 --> 00:08:09,340 I'll do this within the while condition. 76 00:08:09,490 --> 00:08:11,710 So I'm going to say - all right - 77 00:08:11,710 --> 00:08:30,220 Now bear with me: integer.parseInt(message) - and we're going to compare that to secret_number, but we need 78 00:08:30,220 --> 00:08:35,920 that to be not equal to our secret_number. 79 00:08:35,930 --> 00:08:37,040 that to be not equal to our secret_number. 80 00:08:37,130 --> 00:08:40,130 Let me try and explain this a bit better. 81 00:08:40,130 --> 00:08:50,330 So, as long as this condition is met, and this is pretty much saying: "OK so this is what the client is 82 00:08:50,330 --> 00:08:58,490 giving us, and as long as they're not hitting the right number, we need to go back into the loop." 83 00:08:58,490 --> 00:09:01,020 We need to say "Guess a number" 84 00:09:01,070 --> 00:09:05,140 again. That means that they haven't got the right number yet. 85 00:09:05,150 --> 00:09:13,700 So we keep sending them the prompt and the opportunity to guess the number once more. For the first time 86 00:09:13,790 --> 00:09:16,480 when they hit the right number - 87 00:09:16,490 --> 00:09:24,800 - so when they type in the exact number that we have generated here - we're going to get out of this loop. 88 00:09:25,250 --> 00:09:27,710 When we do get out of the loop, 89 00:09:27,770 --> 00:09:40,710 we're going to send out_socket.println("You got it!!!") - for example. You can send whatever you want. 90 00:09:41,220 --> 00:09:52,560 And then I'm going to print out in the servers console "Secret number is out." - for example - "Exiting the 91 00:09:53,040 --> 00:09:56,870 app." - that's hopefully it for the server, 92 00:09:56,970 --> 00:10:05,960 I'm going to save this and switch to the client. The client is going to be guessing a number. I'm going 93 00:10:05,960 --> 00:10:13,630 to call that "user_number", and we're going to be creating a loop here too. 94 00:10:13,670 --> 00:10:24,210 It's going to be a regular while loop, and the condition is going to be: read from the in_socket, 95 00:10:26,310 --> 00:10:33,360 in_socket.readln, and then if that line starts with "Guess", 96 00:10:36,170 --> 00:10:44,780 that means we haven't guessed the number yet, and we're getting into the loop. As long as we're in the 97 00:10:44,780 --> 00:10:45,380 loop, 98 00:10:45,380 --> 00:10:51,890 We're going to be printing out a prompt saying - for example, "Servers says: 99 00:10:54,390 --> 00:10:57,540 Guess a number from 1 to 10." 100 00:11:00,910 --> 00:11:07,030 and then we're going to prompt our user to type in a number. 101 00:11:07,300 --> 00:11:12,610 We're going to store whatever they type in into our user_number string. 102 00:11:12,610 --> 00:11:13,240 We're going to store whatever they type in into our user_number string. 103 00:11:22,490 --> 00:11:29,060 And we're going to simply just send out that string to the server. 104 00:11:29,720 --> 00:11:32,210 We're going to use the out_socket for that. 105 00:11:32,210 --> 00:11:36,560 We're just going to say println(user_number). 106 00:11:39,730 --> 00:11:43,360 As long as we send the wrong number, 107 00:11:43,360 --> 00:11:48,630 we will be getting a message back, saying "Guess a number." 108 00:11:48,700 --> 00:11:57,460 So this condition will be met as long as we're hitting the wrong number, and we will keep going through 109 00:11:57,460 --> 00:11:58,320 this loop. 110 00:11:58,330 --> 00:12:05,860 So as long as we're getting a sentence beginning with a "Guess", we need to be in this loop. As soon as 111 00:12:05,860 --> 00:12:07,090 we get something else, 112 00:12:07,090 --> 00:12:10,330 the other thing we can get is "You got it!!!" - 113 00:12:10,330 --> 00:12:14,270 - we are going to fall out of our loop. 114 00:12:14,350 --> 00:12:21,220 So we're just going to print out when that happens. Obviously, right after this bracket, we're out of the 115 00:12:21,220 --> 00:12:24,340 loop and we can print out: 116 00:12:24,520 --> 00:12:31,150 "You got it!!!". Let's save this and see if it works. 117 00:12:31,200 --> 00:12:37,710 As always, I'm going to run the server first, and then I'm going to run the client. 118 00:12:40,590 --> 00:12:48,360 Ok, let's play the game. So the server is saying - you need to make sure that you're in the client console 119 00:12:48,360 --> 00:12:56,820 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 120 00:12:56,850 --> 00:12:58,990 go from 1 to 10, I'm going to 2, 121 00:12:59,190 --> 00:13:07,420 3, 4, 5, 6 - and I got it! 122 00:13:07,680 --> 00:13:18,250 It's 6. Of course, as soon as you get the right number the next line of code for both the client and the 123 00:13:18,250 --> 00:13:27,370 server is going to be socket.close(). So the socket is going to be closed right after we get this message 124 00:13:27,400 --> 00:13:30,220 that we have hit the right number. 125 00:13:30,550 --> 00:13:38,350 Now this was very basic but hopefully it got you interested to continue to play around with the code. 126 00:13:38,680 --> 00:13:45,910 If you have any questions or issues send me a message or just post it in the Q&A section!