1 00:00:01,320 --> 00:00:09,480 As promised, in this lesson we're going to go through the sender side of our simple UDP program. 2 00:00:09,480 --> 00:00:12,630 Let's get our main method out of the way. 3 00:00:14,660 --> 00:00:17,990 Again, it's going to have the try-catch block. 4 00:00:18,020 --> 00:00:31,180 We're going to use e.printStackTrace() in the catch block, and then new Sender() in the try block. 5 00:00:31,210 --> 00:00:34,840 We're going to define our Sender() method here, 6 00:00:41,360 --> 00:00:49,880 and then just like on the receiver side, we need out socket. We're gonna call it "socket", 7 00:00:55,690 --> 00:01:02,080 and since this is a client socket we don't need to forward any port numbers. 8 00:01:02,140 --> 00:01:10,150 I'm gonna press Ctrl + Shift + O to import all the necessary packets, and then I'm going to create a Scanner. 9 00:01:20,810 --> 00:01:30,250 Again, Ctrl + Shift + O, and here we can start with the sending and receiving processes. 10 00:01:30,300 --> 00:01:32,190 So this is a sender, 11 00:01:32,220 --> 00:01:38,110 so the first thing this program is going to do is - it's going to send a message. 12 00:01:38,130 --> 00:01:45,110 So I want to prompt the user to type in the message that that they want to send. 13 00:01:45,120 --> 00:01:55,140 So I'm gonna say "Enter your message: " and then I'm going to store that message in into message string, 14 00:02:05,360 --> 00:02:11,240 and then we need to move that string into an array of bytes. 15 00:02:11,240 --> 00:02:19,910 We're going to call that "buffer", and we want to get the bytes out of our string. 16 00:02:19,980 --> 00:02:22,550 Next we're going to define our packet. 17 00:02:22,590 --> 00:02:26,150 Again, it's going to be DatagramPacket. 18 00:02:26,160 --> 00:02:36,990 We're going to name it "packet", and we're going to say - new DatagramPacket, and this time we're sending 19 00:02:37,350 --> 00:02:45,720 >>buffer<<, where we have our message, >>buffer.length<<, the IP address - 20 00:02:48,940 --> 00:02:56,030 we're going to use .getByName, and then instead of typing "localhost" - we can do that too - we're going 21 00:02:56,030 --> 00:02:58,040 to type the IP address. 22 00:03:00,660 --> 00:03:05,480 And then after that, we need the port number of the receiver. 23 00:03:06,570 --> 00:03:08,430 And that's pretty much it. 24 00:03:08,460 --> 00:03:09,690 That's our packet. 25 00:03:09,690 --> 00:03:12,980 The only thing I'm missing is Shift + Ctrl + O. 26 00:03:13,030 --> 00:03:26,770 Now we can just send our packet through the socket, and I'm going to print out a message saying - 27 00:03:31,360 --> 00:03:32,060 all right. 28 00:03:32,090 --> 00:03:35,710 That was the sending part. 29 00:03:35,720 --> 00:03:39,290 Now we can focus on the receiving part. 30 00:03:39,530 --> 00:03:48,470 I'm going to create a new array of bytes, since the previous one has been filled - so I'm gonna say new 31 00:03:48,470 --> 00:03:52,700 byte[], and the length is going to be 1500 bytes. 32 00:03:55,680 --> 00:03:58,350 And then I'm going to define a new packet - 33 00:04:02,570 --> 00:04:07,460 the packet needs to have a buffer and we need to know what's the length. 34 00:04:10,820 --> 00:04:12,620 And right after that, 35 00:04:12,620 --> 00:04:15,690 I'm ready to receive my packet. 36 00:04:15,710 --> 00:04:23,570 So I'm just gonna say .receive - and then we're going to store the packet that we received in to our packet object. 37 00:04:23,600 --> 00:04:24,580 So I'm just gonna say .receive - and then we're going to store the packet that we received in to our packet object. 38 00:04:24,590 --> 00:04:34,700 The only thing left to do now is to transfer the data from our buffer into our message string. 39 00:04:34,700 --> 00:04:44,800 I'm gonna say >>buffer<< here, and then we need to use that trim() method, and then we can print out what we 40 00:04:44,800 --> 00:04:45,550 received - 41 00:04:52,490 --> 00:04:53,940 that should be it. 42 00:04:54,060 --> 00:04:57,050 Let's save this and then try it out. 43 00:04:57,050 --> 00:05:03,630 I'm going to run the receiver first and then I'm going to run the sender 44 00:05:06,370 --> 00:05:08,750 I'm going to say "hi" 45 00:05:08,960 --> 00:05:14,890 OK so I can see at the receiver that they have received my message. 46 00:05:14,950 --> 00:05:27,350 I'm going to say "bye" - and this is it. Both the Receiver and the Sender are terminated, because we have reached 47 00:05:27,380 --> 00:05:30,040 the end of our programs. 48 00:05:30,260 --> 00:05:35,550 And as you can notice, we don't have the socket.close() here. 49 00:05:35,570 --> 00:05:44,870 That's because we don't have a socket open and that is pretty much because in UDP, you don't have a connection 50 00:05:45,140 --> 00:05:49,310 that is established before you send any data. 51 00:05:49,310 --> 00:05:58,760 So in this program, we have pretty much sent each packet on its own, which is why the code looks like 52 00:05:58,790 --> 00:06:01,150 this. In the next lesson, 53 00:06:01,160 --> 00:06:09,320 I'm going to explain it even further, and we're going to add something to make this communication go 54 00:06:09,410 --> 00:06:09,920 on. 55 00:06:09,920 --> 00:06:18,530 So we want these two programs to be able to continue to communicate and we'll see how that's done in 56 00:06:18,530 --> 00:06:19,760 the next lesson.