1 00:00:00,930 --> 00:00:06,780 In this section, we talk about UDP socket programming in Java. 2 00:00:06,780 --> 00:00:13,640 First, let's remind ourselves of the difference between TCP and UDP. 3 00:00:13,650 --> 00:00:22,890 They are both transport-layer protocols, and the main difference between them is that TCP requires 4 00:00:22,950 --> 00:00:29,300 a connection to be established before communicating, but UDP doesn't. 5 00:00:29,340 --> 00:00:38,400 From the perspective of coding network applications, in both cases we use sockets because we need 6 00:00:38,520 --> 00:00:42,030 a door through which we're going to send data. 7 00:00:42,210 --> 00:00:49,390 So both in TCP and UDP, we use a socket, and we have port numbers. 8 00:00:49,500 --> 00:00:57,640 But the difference is that in UDP there is no clear establishment of a connection. 9 00:00:57,660 --> 00:01:02,850 We just send every packet separately and hope for the best. 10 00:01:02,910 --> 00:01:06,390 Seriously - that's how UDP works :) 11 00:01:06,390 --> 00:01:08,100 There is no flow control, 12 00:01:08,100 --> 00:01:13,940 congestion control, no sequence, no acknowledgement - none of that. 13 00:01:13,950 --> 00:01:23,100 We just fill out these four fields, and fill the payload with some data, send it out and hope for the 14 00:01:23,100 --> 00:01:24,090 best. 15 00:01:24,090 --> 00:01:27,140 All right. Let's create a new project for this. 16 00:01:27,150 --> 00:01:31,250 I'm going to do a right click in the Package Explorer area. 17 00:01:31,500 --> 00:01:33,950 Go to New -> Java Project. 18 00:01:34,290 --> 00:01:41,790 Let's call it Simple_UDP. 19 00:01:41,800 --> 00:01:44,140 Now we're going to need a new packet, 20 00:01:53,270 --> 00:01:56,540 and then I'm going to create two classes. 21 00:01:56,540 --> 00:01:58,670 We're going to have a Receiver 22 00:02:03,700 --> 00:02:08,040 and a Sender. 23 00:02:08,100 --> 00:02:15,360 We're going to create pretty much the same program we created in our first exercise, where the sender 24 00:02:15,360 --> 00:02:22,350 will send a message to the receiver, and then the receiver will send something back, and that's going 25 00:02:22,350 --> 00:02:23,720 to be it. 26 00:02:23,790 --> 00:02:27,420 We're going to start from the Receiver. 27 00:02:27,420 --> 00:02:34,860 The first thing we're going to need here is going to be - of course - the inevitable main method. And we're 28 00:02:34,860 --> 00:02:41,220 going to use try-catch here too. In the catch block, 29 00:02:41,290 --> 00:02:50,710 I'm going to use e.printStackTrace(), and then in the try block, I'm going to say new Receiver(). 30 00:02:54,230 --> 00:02:56,310 Let's go and create that method. 31 00:02:56,990 --> 00:03:02,930 It's going to be public Receiver() and we need an exception here - 32 00:03:07,880 --> 00:03:11,960 we'll start from creating a DatagramSocket. 33 00:03:22,130 --> 00:03:23,740 Ss you can probably guess, 34 00:03:23,750 --> 00:03:30,400 This is similar to our server socket, because we are defining our port number here. 35 00:03:30,410 --> 00:03:38,270 That's because even though we don't technically have a client or a server, we still need to reserve a 36 00:03:38,270 --> 00:03:47,210 port number in our operating system, and also an application - or rather a network application - has to have 37 00:03:47,270 --> 00:03:49,330 a port number. Regardless, 38 00:03:49,580 --> 00:03:54,640 there is going to be a lesson where I'm going to go through the code again, 39 00:03:54,650 --> 00:03:59,290 so don't worry - I will clear up the details later. 40 00:03:59,330 --> 00:04:05,530 I'm going to press Ctrl + Shift + O to import the DatagramSocket package. 41 00:04:05,600 --> 00:04:07,250 And now we can move on. 42 00:04:07,250 --> 00:04:10,820 The first thing I'm gonna do is - I'm going to print out 43 00:04:13,660 --> 00:04:26,280 "Receiver is running", and then we're going to need a Scanner here just like we had in our previous examples. 44 00:04:26,340 --> 00:04:28,110 I'm going to name it "keyboard", 45 00:04:34,200 --> 00:04:37,880 and that's going to come from System input. 46 00:04:38,250 --> 00:04:41,600 And we're also going to need Ctrl + Shift + O 47 00:04:41,610 --> 00:04:46,050 here. Now comes the messy part. 48 00:04:46,050 --> 00:04:56,340 Since there are no streams in UDP, we have to pretty much build our own fields in the packet, and also 49 00:04:56,340 --> 00:04:57,540 the payload. 50 00:04:57,540 --> 00:05:03,870 So the payload is sent as bytes, and the other fields - 51 00:05:03,870 --> 00:05:10,090 - let's just go through them - source port: your computer knows which source sport it's using, 52 00:05:10,100 --> 00:05:15,390 so we don't need to define that, but we do need to define destination port. 53 00:05:15,390 --> 00:05:24,150 We do need to define the length of the data, and then "Header and Data Checksum" also is done by your 54 00:05:24,180 --> 00:05:25,650 operating system. 55 00:05:25,650 --> 00:05:36,060 So there are some tricky things here so you're going to have to just bear with me and I promise to explain 56 00:05:36,210 --> 00:05:40,590 all of this code in even more detail in a separate lesson. 57 00:05:40,590 --> 00:05:48,780 We're going to create a buffer here, and this buffer is going to be an array of bytes. 58 00:05:48,810 --> 00:06:00,380 I'm going to say >>byte[]<< and we're gonna call that "buffer", and we're going to say "new bytes", and the size 59 00:06:00,380 --> 00:06:05,190 of this array is 1500. 60 00:06:05,240 --> 00:06:08,080 This number is not accidental, 61 00:06:08,090 --> 00:06:17,450 it's called the MTU - Maximum Transmission Unit, and 1500 bytes is the limit of how much 62 00:06:17,450 --> 00:06:20,990 data you can transfer in a single packet. 63 00:06:20,990 --> 00:06:23,370 Now we need to create a packet. 64 00:06:23,420 --> 00:06:32,540 Think of it as - creating a frame where you're going to place your data. So it's DatagramPacket - 65 00:06:32,540 --> 00:06:41,900 We're going to call it - packet = new DatagramPacket, and then we need the buffer in which we're going 66 00:06:41,900 --> 00:06:54,490 to place the data, and then we need buffer length... Since we have created an array of 1500 bytes, 67 00:06:54,540 --> 00:07:02,490 we could just say "1500" instead of "buffer.length", but let's do it properly. 68 00:07:02,490 --> 00:07:05,170 I'm going to press Ctrl + Shift + O to 69 00:07:05,190 --> 00:07:15,160 import the DatagramPacket package, and then we're going to move on to socket.receive(). At this point, 70 00:07:15,180 --> 00:07:25,320 we are receiving the message from the Sender, and we want to place that message into our newly created 71 00:07:25,320 --> 00:07:26,320 packets. 72 00:07:26,340 --> 00:07:28,080 I'm going to add a comment here - 73 00:07:34,850 --> 00:07:39,740 and now we need to extract a string from that message. 74 00:07:39,740 --> 00:07:48,100 So our message is in this object, and the message is actually in this buffer part. 75 00:07:48,140 --> 00:07:51,020 We want to create a string. 76 00:07:51,140 --> 00:08:02,800 We're going to call that string "message" and we're going to say new String - take the data from buffer, 77 00:08:03,190 --> 00:08:11,440 and then trim that data. We're using trim() here because there's a huge possibility that the sender typed 78 00:08:11,470 --> 00:08:19,180 in something much shorter than 1500 bytes. If you remember from one of our previous examples, 79 00:08:19,240 --> 00:08:22,070 "Welcome!" was only 10 bytes. 80 00:08:22,120 --> 00:08:31,440 So we're using this method to get rid of any unnecessary data that comes after our sender's message. 81 00:08:31,510 --> 00:08:38,820 Now that we've got the string, there's nothing stopping us from printing that string out. 82 00:08:38,890 --> 00:08:41,530 We're going to say "Received: " - 83 00:08:45,390 --> 00:08:53,070 and then display the message. At this point we're pretty much done with the receiving part. 84 00:08:53,070 --> 00:09:01,200 Now we need to focus on the sending part. In order to know where to send our response, 85 00:09:01,200 --> 00:09:09,090 we need to get the IP address of the sender, and the port number that the sender used. Here I'm talking 86 00:09:09,090 --> 00:09:16,440 about the source number that the sender used, only this time, we're going to use their source number as 87 00:09:16,530 --> 00:09:18,470 our destination number. 88 00:09:18,570 --> 00:09:21,930 This data is stored in the packet itself. 89 00:09:21,930 --> 00:09:32,250 I'm going to say InetAddress senders_address - and then get that from the packet. 90 00:09:35,380 --> 00:09:38,470 And then senders_port is an integer - 91 00:09:43,350 --> 00:09:45,620 .getPort(); 92 00:09:47,070 --> 00:09:53,100 Ctrl + Shift + O to import the packages, and we can focus on the sending part 93 00:09:53,100 --> 00:09:57,710 now. We want our user to enter their message, 94 00:10:03,260 --> 00:10:08,460 and then we're going to store their message into our message string. 95 00:10:08,480 --> 00:10:15,800 We're going to say keyboard - 96 00:10:15,980 --> 00:10:22,160 now we need to switch that message - or that string - into bytes. 97 00:10:22,160 --> 00:10:32,060 We're going to fill our buffer with that message by saying "message" - and then .getBytes() out of my message. 98 00:10:32,060 --> 00:10:37,350 Now we need to fill out our packets. 99 00:10:37,570 --> 00:10:46,120 We're going to create a new packet, and what we need is the buffer. 100 00:10:46,120 --> 00:10:49,360 Our message is actually in the buffer. 101 00:10:49,510 --> 00:10:55,510 We need to say what's the length of our data. 102 00:10:55,570 --> 00:10:57,430 We need to say what's - 103 00:11:01,350 --> 00:11:02,070 OK. 104 00:11:02,240 --> 00:11:14,030 - we need to say what's the IP address where we're sending the message, and also what's the port. All there 105 00:11:14,030 --> 00:11:18,360 is left to do is to send out that packet. 106 00:11:18,560 --> 00:11:22,290 We're going to use socket.send - 107 00:11:22,790 --> 00:11:25,080 and then we're sending our packet. 108 00:11:25,190 --> 00:11:30,420 I'm going to add a printout just to track where we are. 109 00:11:30,470 --> 00:11:36,590 We're going to say "Sent: " and then display the message that we sent. 110 00:11:36,650 --> 00:11:41,420 That's it for the receiver. Of course to be able to test this program, 111 00:11:41,420 --> 00:11:43,370 we need to create the sender. 112 00:11:43,370 --> 00:11:46,460 So we'll do that in the next lesson. 113 00:11:46,460 --> 00:11:47,390 See you there.