In this section, we talk about UDP socket programming in Java.
First, let's remind ourselves of the difference between TCP and UDP.
They are both transport-layer protocols, and the main difference between them is that TCP requires
a connection to be established before communicating, but UDP doesn't.
From the perspective of coding network applications, in both cases we use sockets because we need
a door through which we're going to send data.
So both in TCP and UDP, we use a socket, and we have port numbers.
But the difference is that in UDP there is no clear establishment of a connection.
We just send every packet separately and hope for the best.
Seriously - that's how UDP works :)
There is no flow control,
congestion control, no sequence, no acknowledgement - none of that.
We just fill out these four fields, and fill the payload with some data, send it out and hope for the
best.
All right. Let's create a new project for this.
I'm going to do a right click in the Package Explorer area.
Go to New -> Java Project.
Let's call it Simple_UDP.
Now we're going to need a new packet,
and then I'm going to create two classes.
We're going to have a Receiver
and a Sender.
We're going to create pretty much the same program we created in our first exercise, where the sender
will send a message to the receiver, and then the receiver will send something back, and that's going
to be it.
We're going to start from the Receiver.
The first thing we're going to need here is going to be - of course - the inevitable main method. And we're
going to use try-catch here too. In the catch block,
I'm going to use e.printStackTrace(), and then in the try block, I'm going to say new Receiver().
Let's go and create that method.
It's going to be public Receiver() and we need an exception here -
we'll start from creating a DatagramSocket.
Ss you can probably guess,
This is similar to our server socket, because we are defining our port number here.
That's because even though we don't technically have a client or a server, we still need to reserve a
port number in our operating system, and also an application - or rather a network application - has to have
a port number. Regardless,
there is going to be a lesson where I'm going to go through the code again,
so don't worry - I will clear up the details later.
I'm going to press Ctrl + Shift + O to import the DatagramSocket package.
And now we can move on.
The first thing I'm gonna do is - I'm going to print out
"Receiver is running", and then we're going to need a Scanner here just like we had in our previous examples.
I'm going to name it "keyboard",
and that's going to come from System input.
And we're also going to need Ctrl + Shift + O
here. Now comes the messy part.
Since there are no streams in UDP, we have to pretty much build our own fields in the packet, and also
the payload.
So the payload is sent as bytes, and the other fields -
- let's just go through them - source port: your computer knows which source sport it's using,
so we don't need to define that, but we do need to define destination port.
We do need to define the length of the data, and then "Header and Data Checksum" also is done by your
operating system.
So there are some tricky things here so you're going to have to just bear with me and I promise to explain
all of this code in even more detail in a separate lesson.
We're going to create a buffer here, and this buffer is going to be an array of bytes.
I'm going to say >>byte[]<< and we're gonna call that "buffer", and we're going to say "new bytes", and the size
of this array is 1500.
This number is not accidental,
it's called the MTU - Maximum Transmission Unit, and 1500 bytes is the limit of how much
data you can transfer in a single packet.
Now we need to create a packet.
Think of it as - creating a frame where you're going to place your data. So it's DatagramPacket -
We're going to call it - packet = new DatagramPacket, and then we need the buffer in which we're going
to place the data, and then we need buffer length... Since we have created an array of 1500 bytes,
we could just say "1500" instead of "buffer.length", but let's do it properly.
I'm going to press Ctrl + Shift + O to
import the DatagramPacket package, and then we're going to move on to socket.receive(). At this point,
we are receiving the message from the Sender, and we want to place that message into our newly created
packets.
I'm going to add a comment here -
and now we need to extract a string from that message.
So our message is in this object, and the message is actually in this buffer part.
We want to create a string.
We're going to call that string "message" and we're going to say new String - take the data from buffer,
and then trim that data. We're using trim() here because there's a huge possibility that the sender typed
in something much shorter than 1500 bytes. If you remember from one of our previous examples,
"Welcome!" was only 10 bytes.
So we're using this method to get rid of any unnecessary data that comes after our sender's message.
Now that we've got the string, there's nothing stopping us from printing that string out.
We're going to say "Received: " -
and then display the message. At this point we're pretty much done with the receiving part.
Now we need to focus on the sending part. In order to know where to send our response,
we need to get the IP address of the sender, and the port number that the sender used. Here I'm talking
about the source number that the sender used, only this time, we're going to use their source number as
our destination number.
This data is stored in the packet itself.
I'm going to say InetAddress senders_address - and then get that from the packet.
And then senders_port is an integer -
.getPort();
Ctrl + Shift + O to import the packages, and we can focus on the sending part
now. We want our user to enter their message,
and then we're going to store their message into our message string.
We're going to say keyboard -
now we need to switch that message - or that string - into bytes.
We're going to fill our buffer with that message by saying "message" - and then .getBytes() out of my message.
Now we need to fill out our packets.
We're going to create a new packet, and what we need is the buffer.
Our message is actually in the buffer.
We need to say what's the length of our data.
We need to say what's -
OK.
- we need to say what's the IP address where we're sending the message, and also what's the port. All there
is left to do is to send out that packet.
We're going to use socket.send -
and then we're sending our packet.
I'm going to add a printout just to track where we are.
We're going to say "Sent: " and then display the message that we sent.
That's it for the receiver. Of course to be able to test this program,
we need to create the sender.
So we'll do that in the next lesson.
See you there.