1 00:00:00,570 --> 00:00:04,890 In this lesson we're going to work on the client side. 2 00:00:05,070 --> 00:00:12,070 Let's create our class by clicking on simple_tcp -> New -> Class. 3 00:00:12,090 --> 00:00:21,730 I'm going to name it simply "Client". Let's start with adding our main method, the same way we did it for 4 00:00:21,730 --> 00:00:22,620 the server. 5 00:00:22,720 --> 00:00:30,280 So I'm going to type in main, press Ctrl + Space, and then Enter, and then I'm going to add the try-catch 6 00:00:30,280 --> 00:00:38,350 block - I'm going to type in "try" and then press Ctrl + Space, and then Enter. And then we're going to initiate 7 00:00:38,410 --> 00:00:52,660 new Client() in the try block. And then in the catch block I'm going to say e.PrintStackTrace(). We 8 00:00:52,660 --> 00:00:54,640 can create our client method 9 00:00:54,640 --> 00:01:01,300 now. It's going to be public Client() throws Exception 10 00:01:04,130 --> 00:01:08,510 - and in this method is going to be our application. 11 00:01:08,540 --> 00:01:12,270 So this is going to be our client side of our program. 12 00:01:12,440 --> 00:01:16,870 And the first thing we need to do here is to create a socket. 13 00:01:16,940 --> 00:01:20,200 This time we only have one type of socket. 14 00:01:20,210 --> 00:01:26,900 On the server side, we had two types of sockets: we had the server socket and then the regular communication 15 00:01:26,900 --> 00:01:27,600 socket. 16 00:01:27,620 --> 00:01:31,900 So on the client side we're only going to have a regular socket. 17 00:01:31,970 --> 00:01:38,060 This socket is going to be - we're going to use the Socket() method again. 18 00:01:38,060 --> 00:01:48,630 So it's going to be Socket socket = new Socket(); and then we're going to forward two arguments to this 19 00:01:48,630 --> 00:01:49,080 method. 20 00:01:49,080 --> 00:01:54,110 It's going to be the IP address of the server and the listening port 21 00:01:54,120 --> 00:02:04,380 - so the port number where our server is expecting our connection. I'm going to use localhost, since my 22 00:02:04,380 --> 00:02:13,170 server is on this machine. And I'm going to use 2020, which we defined as our port number. 23 00:02:13,200 --> 00:02:21,600 Additionally, I'm going to press Ctrl + Shift + O, to import the java.net.socket packet, so 24 00:02:21,600 --> 00:02:30,360 that we don't have that error anymore. And I'm going to print out a message in the console, saying 25 00:02:34,570 --> 00:02:47,900 "Successful connection to the server". This printout right here is useful to us because if something fails 26 00:02:47,990 --> 00:02:53,290 in this line we're going to be thrown to the catch block. 27 00:02:53,330 --> 00:03:03,590 So, by reaching this line of code - by seeing this sentence in our console - we know that we have successfully 28 00:03:03,590 --> 00:03:05,630 passed this line of code. 29 00:03:05,690 --> 00:03:14,400 So, this really reassures us that everything went well and that we actually are connected to our server. 30 00:03:14,420 --> 00:03:18,860 Next thing we need to do here is create the input and output buffers. 31 00:03:18,860 --> 00:03:24,160 Now luckily for us, they're exactly the same as on the server side. 32 00:03:24,260 --> 00:03:31,820 So you can just copy them, but I'm going to type them out real quick. For the incoming data, 33 00:03:31,830 --> 00:03:36,120 we're going to need BufferedReader. 34 00:03:36,120 --> 00:03:50,780 We're going to name that in_socket = new BufferedReader (new InputStreamReader - and then socket.getInput 35 00:03:50,780 --> 00:03:51,590 - socket.getInputStream - 36 00:03:54,550 --> 00:03:58,780 and then for the outgoing socket, it's going to be PrintWriter - 37 00:04:01,720 --> 00:04:20,080 PrintWriter = out_socket (new PrintWriter... 38 00:04:20,170 --> 00:04:21,770 There it is. 39 00:04:21,820 --> 00:04:26,950 I'm going to add a comment here just to separate this section, 40 00:04:29,560 --> 00:04:37,950 and press Ctrl + Shift + O to import all the all the required packets. All right. 41 00:04:38,030 --> 00:04:42,610 These are the basic requirements for our client-server communication. 42 00:04:42,710 --> 00:04:47,200 And now what we need to worry about is the protocol. 43 00:04:47,200 --> 00:04:56,500 A protocol is pretty much just a set of rules by which communication between two or more sides occurs. 44 00:04:56,510 --> 00:05:03,820 So we need to define who is the first to send a message, what kind of message, what kind of responses 45 00:05:03,830 --> 00:05:06,780 are expected, etc etc. 46 00:05:07,070 --> 00:05:12,320 For our simple little program we're going to have a simple little protocol. 47 00:05:12,470 --> 00:05:19,970 So the client is going to connect to the server, the server is going to say "Welcome!", the client is then 48 00:05:19,970 --> 00:05:24,730 going to say "Thanks!", and then they're going to disconnect. 49 00:05:24,890 --> 00:05:29,180 We have already programmed the server to do all of these things. 50 00:05:29,270 --> 00:05:31,770 So it's going to be listening for connections, 51 00:05:31,850 --> 00:05:38,570 and then once the connection arrives, it's going to accept the connection, and then right away it's going 52 00:05:38,570 --> 00:05:41,030 to send out "Welcome!" 53 00:05:41,120 --> 00:05:44,320 It's going to then receive the message from the client. 54 00:05:44,330 --> 00:05:48,970 It's going to display it in its console, and then it's going to close the socket. 55 00:05:48,980 --> 00:06:02,690 So socket.close() is actually the line where we initiate disconnection or - interruption of the connection. 56 00:06:02,730 --> 00:06:03,170 All right. 57 00:06:03,180 --> 00:06:05,120 Let's go back to our client. 58 00:06:05,150 --> 00:06:10,530 So this is where the protocol actually takes place. First, 59 00:06:10,530 --> 00:06:16,170 we need to receive the message from the server. We're going to store that message in a string called 60 00:06:16,320 --> 00:06:18,960 "message" - very original. 61 00:06:18,960 --> 00:06:28,010 We're going to read that message from our socket, with "readLine()", and then we can display that message 62 00:06:28,010 --> 00:06:29,470 in our console. 63 00:06:29,510 --> 00:06:36,410 We're going to say "Server says:" and then add the message. 64 00:06:36,410 --> 00:06:39,960 Now we need to send "Thank you" back to the server. 65 00:06:40,010 --> 00:06:46,430 We're going to do that by using our out_socket object and println, 66 00:06:49,500 --> 00:06:51,360 and we're going to say "Thanks!" 67 00:06:53,860 --> 00:06:55,450 That's actually pretty much it. 68 00:06:55,450 --> 00:07:01,660 So according to our protocol, we need to connect to the server, receive the "Welcome!" message, send a 69 00:07:01,660 --> 00:07:04,870 "Thanks!" message, and then disconnect. 70 00:07:04,870 --> 00:07:11,650 So we're going to disconnect by closing the socket: socket .close(), and we're going to 71 00:07:11,650 --> 00:07:22,890 print out a message in our console, saying "Socket closed". Now I'm going to save this and try and run it 72 00:07:23,040 --> 00:07:24,760 and see if it works. 73 00:07:25,080 --> 00:07:33,570 First, we're going to run the server, so you run the server by pressing - or choosing - the server class, and 74 00:07:33,570 --> 00:07:38,170 then pressing "Play" or "Run" in Eclipse. 75 00:07:38,340 --> 00:07:43,260 And then I'm going to choose the client class, and also press Run. 76 00:07:43,350 --> 00:07:50,580 So the program is going to just go through all of the code by itself, since we've already typed in all the 77 00:07:50,580 --> 00:07:52,590 messages that need to be sent. 78 00:07:52,920 --> 00:07:56,820 But let's just quickly go over what happened. 79 00:07:56,850 --> 00:08:07,740 In order to switch between console messages for server and client, you need to press this little icon 80 00:08:07,800 --> 00:08:10,070 of a display here. 81 00:08:10,140 --> 00:08:15,410 So right now we're looking at the server console, and we can see all of our messages. 82 00:08:15,420 --> 00:08:18,470 So ports 2020 is open. 83 00:08:18,500 --> 00:08:21,080 We got that right here. 84 00:08:21,110 --> 00:08:23,020 So we get that message 85 00:08:23,030 --> 00:08:32,240 once we successfully reserve a new port in our operating system, and then when a client connects, we get 86 00:08:32,240 --> 00:08:33,250 this message. 87 00:08:33,260 --> 00:08:41,290 This is the IP address of our client - 127.0.0.1 - that's the loopback address, 88 00:08:41,510 --> 00:08:46,490 and then we get "Client says: Thanks!" 89 00:08:46,500 --> 00:08:49,500 So we have read the client's message. 90 00:08:49,500 --> 00:08:53,310 But before that, we sent "Welcome!" to the client. 91 00:08:53,310 --> 00:08:58,840 So this actually happened before this. 92 00:08:58,970 --> 00:08:59,720 All right. 93 00:08:59,730 --> 00:09:08,190 As soon as these messages were exchanged, we've closed the sockets on both sides. 94 00:09:08,190 --> 00:09:11,460 So that's when we got "Socket is closed." 95 00:09:11,460 --> 00:09:15,210 message, both on the server and on the client.