1 00:00:01,700 --> 00:00:05,470 Let's start with creating a new Java project. 2 00:00:05,480 --> 00:00:15,050 I'm going to go to the package explorer area, right click -> New -> Java Project. Let's name it "Simple_TCP" for 3 00:00:15,050 --> 00:00:27,670 example. Open that, right click on the source package, New -> Package. "simple_tcp". And then I'm going to do 4 00:00:27,760 --> 00:00:32,470 a right click on that, and create our first class. 5 00:00:33,280 --> 00:00:39,580 Our class is going to be called "Server", and that's actually going to be all the code we're going to need 6 00:00:39,580 --> 00:00:42,780 for our server side of our application. 7 00:00:42,820 --> 00:00:49,780 Now, I'm going to explain every single line of code we use here in a separate lesson, so if you're familiar 8 00:00:49,780 --> 00:00:56,470 with Java you can just skip that, but for now, I'm going to give you just enough information so that you 9 00:00:56,470 --> 00:00:59,300 can understand what we're doing here. 10 00:00:59,320 --> 00:01:06,400 The first thing we're going to do is we're going to create a main method. A main method is an entry point 11 00:01:06,430 --> 00:01:11,410 to your application, and there's actually a shortcut for this. 12 00:01:11,410 --> 00:01:17,200 In Java, you just type in "main", press Ctrl + Space, 13 00:01:17,200 --> 00:01:20,140 press Enter, and there you have it. 14 00:01:20,350 --> 00:01:29,860 Next, let's add a try-catch block. A try-catch block is basically a piece of code that Java will try to 15 00:01:29,860 --> 00:01:34,420 run, and then if it fails it's going to go to the catch code. 16 00:01:34,840 --> 00:01:41,470 So the way you do this - there's also a shortcut for this - you type in "try" and then press Ctrl + Space 17 00:01:41,560 --> 00:01:44,950 and then press Enter. In the try block, 18 00:01:44,980 --> 00:01:47,620 I'm going to call new Server(); 19 00:01:50,350 --> 00:02:00,010 Server() is going to be a method - public Server() - where pretty much all of our code is going to be located, 20 00:02:00,010 --> 00:02:06,520 and then if anything goes wrong in this area here, we're going to be thrown to the catch block. 21 00:02:06,520 --> 00:02:14,960 And what I want the catch block to do is - I want to use this "e" object. 22 00:02:15,100 --> 00:02:17,540 That's the exception object. 23 00:02:17,620 --> 00:02:21,160 And I want it to print out what happens. 24 00:02:21,160 --> 00:02:24,170 So I want to know what went wrong. 25 00:02:24,190 --> 00:02:29,530 And that's what I'm going to get by using this method. 26 00:02:29,560 --> 00:02:30,090 All right. 27 00:02:30,100 --> 00:02:33,220 Now let's focus on creating sockets. 28 00:02:35,940 --> 00:02:42,600 On the server side of the application, we're going to have two types of sockets: a server socket and then 29 00:02:42,660 --> 00:02:52,350 a regular socket. The server socket is just going to hold a port in the operating system for our application. 30 00:02:52,350 --> 00:03:00,620 And here it goes: we're going to use ServerSocket, and then we're going to name it server_socket. 31 00:03:00,660 --> 00:03:06,450 server_socket = new ServerSocket 32 00:03:06,450 --> 00:03:12,780 And then we're going to forward the number of our port. 33 00:03:12,780 --> 00:03:14,500 I'm going to use 2020. 34 00:03:14,520 --> 00:03:26,370 Looking into the future here ;) - and I'm going to comment that with "opening a new port". We're getting this 35 00:03:26,430 --> 00:03:32,840 error because we need to import a packet that contains this ServerSocket method. 36 00:03:33,000 --> 00:03:36,380 And that packet is java.net. 37 00:03:36,480 --> 00:03:39,920 But don't worry, there's a shortcut for this too. 38 00:03:39,930 --> 00:03:42,120 You just need to press Ctrl + Shift + O, 39 00:03:42,130 --> 00:03:47,680 and here it is - java.net.ServerSocket. 40 00:03:47,700 --> 00:03:52,490 Now this second error is because we need to throw an exception here 41 00:03:56,650 --> 00:04:03,540 and we're just going to skip this because I'll explain that later, in this explanatory lesson. 42 00:04:03,790 --> 00:04:12,460 I'm just gonna use this to avoid the markups that Java shows when when you have an error like that. 43 00:04:12,460 --> 00:04:16,420 All right. Here I'm going to say "print" - or I'm not gonna do that, 44 00:04:16,420 --> 00:04:22,290 I'm also going to use a shortcut: "syso" + Ctrl + Shift. 45 00:04:22,390 --> 00:04:25,540 And there we have System.out.println. 46 00:04:25,540 --> 00:04:38,080 We're going to print out in the console section, that port 2020 has been opened. 47 00:04:38,130 --> 00:04:38,930 All right. 48 00:04:39,030 --> 00:04:46,110 Now we are ready to open this regular communication socket that I've talked about. We're going to use 49 00:04:46,110 --> 00:04:48,140 the Socket method for it. 50 00:04:48,180 --> 00:04:51,380 So it's going to be called "socket", 51 00:04:51,390 --> 00:05:04,170 and we're going to use our ServerSocket, and then call accept(). accept() is a blocking method that accepts 52 00:05:04,230 --> 00:05:05,860 incoming connections. 53 00:05:05,970 --> 00:05:08,610 And what a "blocking method" means - 54 00:05:08,610 --> 00:05:15,840 it means that Java is going to go through our lines of code, and then it's going to stop at this line, 55 00:05:16,080 --> 00:05:23,310 and it's going to say "OK now I'm waiting for an incoming connection to come up", and it's not going to 56 00:05:23,310 --> 00:05:32,330 continue to go through the other lines until an incoming connection actually appears and it is accepted. 57 00:05:32,880 --> 00:05:36,970 Let's quickly import java.net.socket. 58 00:05:36,990 --> 00:05:40,150 So again, Ctrl + Shift + O should do it. 59 00:05:40,170 --> 00:05:42,060 There it is. 60 00:05:42,060 --> 00:05:53,310 Now I'm going to print out a message saying "Client ", and then I'm going to add this client's IP address. 61 00:05:53,310 --> 00:05:56,760 I'm gonna say socket.getInetAddress(), 62 00:05:56,760 --> 00:05:59,580 "Inet" is short for Internet. 63 00:05:59,730 --> 00:06:09,300 And what this method does - it retrieves the client's IP address from the socket object. So socket 64 00:06:09,330 --> 00:06:15,000 object was created right here, when we got an incoming connection that was accepted. 65 00:06:15,000 --> 00:06:24,780 So naturally, this object contains information such as - IP address of our client. 66 00:06:24,780 --> 00:06:36,090 And then I'm going to say "Client " and then their IP address " has connected." 67 00:06:36,110 --> 00:06:39,280 Now we're going to do something a bit more complex. 68 00:06:39,290 --> 00:06:42,720 So we're going to create input and output buffers. 69 00:06:42,740 --> 00:06:46,250 I'm going to add a comment here just to separate this section. 70 00:06:50,380 --> 00:06:56,500 In this lesson we're just going to go real quick through the code and then I'll explain the details 71 00:06:56,770 --> 00:07:04,180 later, but for now, you just need to know that we need a place to store our data - the data that is coming 72 00:07:04,270 --> 00:07:08,230 out of our socket, and the data that is coming into our socket. 73 00:07:08,230 --> 00:07:14,140 We call that place - a buffer. For the input buffer, 74 00:07:14,140 --> 00:07:16,360 we're going to use the Buffered Reader. 75 00:07:18,950 --> 00:07:24,840 And we'll import to these methods or the packets that contain these methods later. 76 00:07:24,920 --> 00:07:34,670 So, the BufferedReader object is going to be "in_socket", since that's going to read the data that's coming 77 00:07:34,760 --> 00:07:36,200 into our socket - 78 00:07:36,200 --> 00:07:49,660 - from the client. new BufferedReader, and then we're going to forward "new InputStreamReader - 79 00:07:50,310 --> 00:07:52,620 - that's going to read data from the socket. 80 00:07:52,620 --> 00:07:56,160 So we need to .getInputStream(). 81 00:08:00,110 --> 00:08:11,030 For our outgoing buffer, we're going to use PrintWriter. We're going to name that out_socket = new PrintWriter, 82 00:08:11,150 --> 00:08:18,660 and then we're going to forward new OutputStreamWriter. 83 00:08:18,690 --> 00:08:29,400 So here we are actually writing the data into the buffer, and then we're going to go socket dot - 84 00:08:29,460 --> 00:08:30,840 socket.getOutputStream() 85 00:08:31,230 --> 00:08:36,990 And here I'm just going to add "true" real quick, and I'll explain what that means 86 00:08:36,990 --> 00:08:45,380 later, I promise. The only thing left to do here is to import the packets containing these methods, 87 00:08:45,530 --> 00:08:47,560 and we're going to get that by pressing 88 00:08:47,560 --> 00:08:48,780 Ctrl + Shift +O 89 00:08:48,800 --> 00:08:50,660 There we go. 90 00:08:50,660 --> 00:08:51,680 That's java.io 91 00:08:51,680 --> 00:08:52,500 java.io 92 00:08:52,510 --> 00:08:52,860 java.io 93 00:08:52,880 --> 00:08:55,580 and then all of these methods. 94 00:08:56,150 --> 00:08:59,450 And now we've got our input and output buffers. 95 00:08:59,450 --> 00:09:07,340 Now we can actually move on with the code. From this moment on, we are deciding what kind of communication 96 00:09:07,340 --> 00:09:13,230 we're going to have between our server and our client. 97 00:09:13,250 --> 00:09:18,970 The first thing I'm going to send as a server is going to be "Welcome!" for example. 98 00:09:18,970 --> 00:09:27,090 So I'm going to use my out socket, out_socket.println - 99 00:09:31,060 --> 00:09:42,960 so our client is going to receive this - I'm going to comment this real quick. 100 00:09:43,090 --> 00:09:44,400 All right. 101 00:09:44,470 --> 00:09:51,790 And then when I receive the message back from the client, I'm going to store that in a string object. 102 00:09:51,820 --> 00:09:59,180 I'm going to call that "message", and I'm going to say read from in_socket. 103 00:09:59,290 --> 00:09:59,990 All right. 104 00:10:00,200 --> 00:10:04,820 Now, I'm going to print out this message in the console. 105 00:10:04,820 --> 00:10:05,870 It's going to be 106 00:10:08,540 --> 00:10:27,000 "Client says:" and then add the message from the client. 107 00:10:27,020 --> 00:10:31,850 This is going to be our entire client-server communication for now. 108 00:10:31,880 --> 00:10:39,260 So, after they're done communicating, the only thing left to do is to close the socket. 109 00:10:39,260 --> 00:10:44,750 So that's what you should always do when the communication is over. 110 00:10:44,750 --> 00:10:51,600 So once the application is going to shut down, we're going to do "socket.close();" 111 00:10:51,770 --> 00:10:59,300 I'm going to comment that too, or - it's not going to be a comment but rather a notification in our console. 112 00:10:59,300 --> 00:11:02,780 I'm going to say "Socket is closed." 113 00:11:02,780 --> 00:11:11,060 So the purpose of these notifications - as I called them throughout this lesson - is to know when we have 114 00:11:11,060 --> 00:11:13,930 passed a certain line of code. 115 00:11:14,180 --> 00:11:20,920 So if we are for any reason stuck in this line, we will never get to this next line. 116 00:11:21,140 --> 00:11:32,440 So if I get a printout "Socket is closed" in my console, I'm going to know that I've - that Java not me - that 117 00:11:32,450 --> 00:11:40,520 Java went through this line, and that everything worked correctly, and that now I'm at line twenty nine. 118 00:11:40,520 --> 00:11:43,920 So this is pretty much it for the server side. 119 00:11:43,940 --> 00:11:50,150 The next lesson is going to be about the client, and then I'm going to do an explanatory lesson where 120 00:11:50,150 --> 00:11:55,520 we're going to explain all of these lines of code even further.