1 00:00:01,190 --> 00:00:08,830 As promised, in this lesson, we're going to go through the code again and explain it with more detail. 2 00:00:08,840 --> 00:00:14,900 We are going to edit the code a bit, so I'm going to add a new package. 3 00:00:17,430 --> 00:00:27,080 I'll name it simple_udp2. And then I'm just going to copy these two classes into this new package. 4 00:00:31,060 --> 00:00:33,580 I'll close the previous two classes 5 00:00:36,780 --> 00:00:42,440 and open the two new ones. 6 00:00:42,460 --> 00:00:49,540 The thing we're going to change is - we're going to allow these two to continue to communicate indefinitely, 7 00:00:49,630 --> 00:00:54,290 but only the sender will be able to enter their message. 8 00:00:54,400 --> 00:01:08,280 The receiver is going to just send - let me see - the receiver can just send "Ok." 9 00:01:08,440 --> 00:01:17,530 This means that I don't really need these lines - I don't need this one and I don't need the Scanner, but 10 00:01:17,620 --> 00:01:25,210 in order to allow them to communicate indefinitely, I'm going to have to add an indefinite loop - and I'm 11 00:01:25,210 --> 00:01:27,360 going to add it right here. 12 00:01:27,400 --> 00:01:36,150 It's going to be >>while<< and the condition is going to be "true", and then I'm going to close this loop right 13 00:01:36,150 --> 00:01:37,080 here. 14 00:01:37,110 --> 00:01:44,940 Now I want to format this better, in order to do that I'm going to select the code that I want to 15 00:01:44,940 --> 00:01:56,630 shift to the right, and I'm going to press Ctrl + Shift + F. "F" is short for "format" and this is what I get 16 00:01:56,630 --> 00:01:58,310 by using it. 17 00:01:58,310 --> 00:01:59,960 So I'm happy with this. 18 00:01:59,960 --> 00:02:04,400 I'm going to save that and move to the Sender. 19 00:02:04,460 --> 00:02:10,730 We need to add the same loop here - and this is where we'll put it. 20 00:02:10,730 --> 00:02:18,890 It's going to be while(true) and we'll close it right here. 21 00:02:18,890 --> 00:02:22,030 We're gonna format this too by pressing 22 00:02:22,030 --> 00:02:25,360 Ctrl + Shift + F. Ok. 23 00:02:25,360 --> 00:02:32,520 That's it - I'm gonna save this, and test if it works. Let's run the Receiver first. 24 00:02:35,330 --> 00:02:36,710 Receiver is running. 25 00:02:36,710 --> 00:02:37,790 That's a good sign. 26 00:02:37,790 --> 00:02:39,960 I'm going to run the Sender 27 00:02:39,980 --> 00:02:49,480 now, and enter something - "hi" - and then, Receiver says: "Ok." 28 00:02:49,660 --> 00:02:56,540 And then I'm going to say "bye" for example, and Receiver again says "Ok." 29 00:02:59,980 --> 00:03:09,340 Now ideally, we would have some kind of verification here - and we would check what did the sender send. 30 00:03:09,340 --> 00:03:19,150 For example, if they send "bye" or "quit" or "exit" - we could break from our loop and end the program. 31 00:03:19,240 --> 00:03:25,870 But I'm going to leave that to you, since we've already done something similar in our previous examples 32 00:03:25,900 --> 00:03:27,080 with TCP, 33 00:03:27,130 --> 00:03:36,250 so you can do the same here, but since this is UDP, and there is no established connection - we can simply 34 00:03:36,250 --> 00:03:44,860 just terminate the sender and then close the console, and then terminate the receiver and close that 35 00:03:44,860 --> 00:03:48,540 console too, without any consequences. 36 00:03:48,550 --> 00:03:50,400 We didn't get any errors. 37 00:03:50,410 --> 00:03:52,350 We didn't get any exceptions. 38 00:03:52,360 --> 00:03:59,490 We were just able to close these two programs without anything really going wrong. 39 00:03:59,500 --> 00:04:06,400 That's because - as I've already mentioned - there are no sessions established between the two. 40 00:04:06,400 --> 00:04:15,670 That's because each packet is built and sent separately, and additionally, we're using UDP - which is pretty 41 00:04:15,670 --> 00:04:18,070 much a "best effort" protocol. 42 00:04:18,070 --> 00:04:24,660 So even if a packet is not delivered to its destination - no worries. 43 00:04:24,760 --> 00:04:28,630 That's how UDP is expected to work. 44 00:04:28,630 --> 00:04:30,730 All right. Let's go through the code. 45 00:04:30,730 --> 00:04:38,460 I'm going to start with the sender, and hopefully you already get the whole point behind the main method. 46 00:04:38,500 --> 00:04:46,930 This is - as I said in our previous, TCP examples - an entry to our application. And then we have the try- 47 00:04:46,930 --> 00:04:55,300 catch block. We try to execute some code in this block, and then if something fails within that code, we 48 00:04:55,300 --> 00:05:04,630 are thrown to the catch block. In the catch block, we've always used this method printStackTrace(), which 49 00:05:04,630 --> 00:05:13,480 pretty much just prints out anything that went wrong - in these red letters in this console area. 50 00:05:13,480 --> 00:05:24,430 So in our try block, we are initiating a new Sender(), and the first thing that happens there is - we create 51 00:05:24,520 --> 00:05:26,650 a DatagramSocket. 52 00:05:26,710 --> 00:05:34,400 This socket is very similar to the regular socket we used in our TCP examples. 53 00:05:34,420 --> 00:05:41,860 It's just a door that we use to throw out the data and receive the new data that's coming in from the 54 00:05:41,860 --> 00:05:42,800 other side. 55 00:05:42,820 --> 00:05:52,320 Then, we have created a System input Scanner that we used to get the data that our user is typing in, 56 00:05:52,330 --> 00:05:54,840 to be sent to the receiver. 57 00:05:54,850 --> 00:06:04,810 So this is where we have used our keyboard scanner to get the message from the user. And then send that 58 00:06:04,810 --> 00:06:09,700 message through the socket. Regarding the while loop, 59 00:06:09,730 --> 00:06:13,310 I added it in this lesson - so you get why I did that. 60 00:06:13,330 --> 00:06:17,200 But let me explain why it's in this place. 61 00:06:17,230 --> 00:06:25,210 I added it here because everything that happens in the body of the loop repeats itself all the time, 62 00:06:25,540 --> 00:06:33,700 and I need these things to repeat because this is the communication between the sender and the receiver. 63 00:06:33,700 --> 00:06:42,700 For example, these two things don't have to happen every time we want to exchange messages between the 64 00:06:42,700 --> 00:06:44,740 sender and the receiver. 65 00:06:44,740 --> 00:06:46,660 They only happen once. 66 00:06:46,720 --> 00:06:51,610 So we need to open a socket only one time. 67 00:06:51,610 --> 00:07:01,360 We have defined our keyboard Scanner only one time. All of the other things do need to happen every single 68 00:07:01,360 --> 00:07:02,170 time 69 00:07:02,170 --> 00:07:09,190 these two want to exchange messages. Every time the sender is sending a message, 70 00:07:09,220 --> 00:07:14,800 we want to print out "Enter your message" to the user. 71 00:07:14,980 --> 00:07:22,220 And then that user enters their message, and we store that message into our message string. 72 00:07:22,420 --> 00:07:31,570 Then we translate that message into bytes, and we store those bytes in our buffer object. 73 00:07:31,570 --> 00:07:40,320 Then we take that buffer where our message is stored, and we put it into our packet. 74 00:07:40,330 --> 00:07:46,570 So what we need to put into the UDP packet - is the payload itself. 75 00:07:46,570 --> 00:07:48,490 So the payload is the message. 76 00:07:48,490 --> 00:07:54,370 That's the useful application data that we are transferring between the two applications. 77 00:07:54,400 --> 00:07:57,930 Then, we need to specify the amount of our data. 78 00:07:57,970 --> 00:07:59,080 That's the length. 79 00:07:59,080 --> 00:08:05,020 And if we look at the UDP header format - this is the field I'm talking about. 80 00:08:05,020 --> 00:08:14,270 So this is the Length field, and then we are specifying the IP address that we're communicating with, 81 00:08:14,750 --> 00:08:17,630 and then we're specifying the destination port. 82 00:08:17,630 --> 00:08:25,230 As soon as we have formed that packet, we can send it out through our socket. 83 00:08:25,280 --> 00:08:34,880 This line below is just a printout for our own console where we are saying "this is what was sent" and 84 00:08:34,880 --> 00:08:38,880 then the rest of the code is the receiving part. 85 00:08:38,930 --> 00:08:41,980 So we needed all of this to send a packet. 86 00:08:42,200 --> 00:08:46,780 But we're going to use all of this to receive a packet. 87 00:08:46,880 --> 00:08:49,580 Let's go through it line by line. 88 00:08:49,580 --> 00:08:54,770 Here we are using the same buffer we used to send a message, 89 00:08:54,800 --> 00:09:03,250 and keep in mind that at this very point - this buffer actually has this message in it. 90 00:09:03,260 --> 00:09:10,070 So that is why we are creating a new array for this very object. 91 00:09:10,070 --> 00:09:15,010 That's because we are going to fill it with the data that we are waiting for. 92 00:09:15,050 --> 00:09:21,840 This is the data that is going to be in the packet that we are receiving here. 93 00:09:21,920 --> 00:09:25,540 Again, we are using an existing object. 94 00:09:25,550 --> 00:09:34,730 So this packet already has some data in it, because we use the same object to send that message that 95 00:09:34,820 --> 00:09:36,830 our user entered. 96 00:09:36,830 --> 00:09:45,320 So now I am creating a new packet that is going to be empty and is going to be ready to receive the 97 00:09:45,320 --> 00:09:46,970 incoming packet. 98 00:09:46,970 --> 00:09:50,090 And that's exactly what I do in this line. 99 00:09:50,090 --> 00:09:59,030 I am looking at the socket and I am saying "Ok, I'm expecting to receive a packet right now. When it comes, 100 00:09:59,360 --> 00:10:02,600 put it into this new object." 101 00:10:02,600 --> 00:10:04,470 So this object isn't new. 102 00:10:04,490 --> 00:10:09,190 It was created earlier to send a packet, 103 00:10:09,290 --> 00:10:13,220 but I have just redefined it in this line here. 104 00:10:13,250 --> 00:10:20,990 And so now it's empty and it's ready to receive the new packet. As soon as I have received the packet, 105 00:10:20,990 --> 00:10:29,920 I'm going to extract the string from the packet. That string - or that message - is inside of a buffer. 106 00:10:29,930 --> 00:10:37,040 So I want to use this new string to again do pretty much the same thing I did here. 107 00:10:37,040 --> 00:10:44,330 I want to reuse this message string, but I'm going to have to extract that message from my buffer. 108 00:10:44,720 --> 00:10:52,940 Additionally, when I extracted the message from the buffer, I need to trim it, because as I mentioned while 109 00:10:52,940 --> 00:11:02,000 we were creating these two classes, this buffer is 1500 bytes long, 110 00:11:02,000 --> 00:11:06,260 and the message that I've received may be shorter. 111 00:11:06,260 --> 00:11:16,670 So this trim() method is used to cut all of that excess data, and then again, the next line is just a printout 112 00:11:16,730 --> 00:11:19,280 of the message that we received. 113 00:11:19,280 --> 00:11:19,930 All right. 114 00:11:19,940 --> 00:11:21,310 That's the sender. 115 00:11:21,320 --> 00:11:23,880 Let's go back to the receiver. 116 00:11:23,900 --> 00:11:30,150 The main method here is again the same main method we've used throughout this course. 117 00:11:30,170 --> 00:11:35,690 So I'm going to skip that. The first thing we see is the DatagramSocket - 118 00:11:35,690 --> 00:11:41,210 It's similar to the sender DatagramSocket. Only at the receiver 119 00:11:41,330 --> 00:11:49,160 end, we have defined the listening port number. And then we've used this printout saying "Receiver is running." 120 00:11:49,160 --> 00:11:57,140 This is just for tracking purposes, to see if we've gone through this line of code correctly. 121 00:11:57,410 --> 00:12:04,460 And then we get into our loop. Here we have the receiving part, and it's very similar to what we have 122 00:12:04,520 --> 00:12:05,620 in the sender. 123 00:12:05,630 --> 00:12:08,300 So I'm going to just skip that. 124 00:12:08,330 --> 00:12:17,360 The only difference we have is this part. At the sender, since the sender is the first one to send a packet, 125 00:12:17,360 --> 00:12:24,740 we had to define the destination IP address and the destination port number. But since the receiver 126 00:12:24,770 --> 00:12:32,690 is just replying to the sender, the receiver can just pull out the sender's IP address. 127 00:12:32,720 --> 00:12:39,480 So the source IP address from the packet, and the senders source port number. 128 00:12:39,500 --> 00:12:48,110 So here - the receiver is getting that information from the packet, and then is using that same information 129 00:12:48,110 --> 00:12:54,010 to create the new packet that it's going to send back to the sender. 130 00:12:54,020 --> 00:12:57,570 That's pretty much everything that happens in this part. 131 00:12:57,650 --> 00:13:06,260 And then we're left with only the printout, which is as always, only giving us a notification in our console 132 00:13:06,290 --> 00:13:10,310 part, to let us know where we're at, and what is happening. 133 00:13:10,320 --> 00:13:15,430 And in this case what was sent to the sender. 134 00:13:15,480 --> 00:13:17,380 That's it for this lesson. 135 00:13:17,400 --> 00:13:22,590 If there's anything else you'd like to know or something you think I could help you with, 136 00:13:22,590 --> 00:13:30,330 send me a message or post it in the Q&A section, and I'll make sure to try and help you if I can.