As promised, in this lesson we're going to go through the sender side of our simple UDP program.
Let's get our main method out of the way.
Again, it's going to have the try-catch block.
We're going to use e.printStackTrace() in the catch block, and then new Sender() in the try block.
We're going to define our Sender() method here,
and then just like on the receiver side, we need out socket. We're gonna call it "socket",
and since this is a client socket we don't need to forward any port numbers.
I'm gonna press Ctrl + Shift + O to import all the necessary packets, and then I'm going to create a Scanner.
Again, Ctrl + Shift + O, and here we can start with the sending and receiving processes.
So this is a sender,
so the first thing this program is going to do is - it's going to send a message.
So I want to prompt the user to type in the message that that they want to send.
So I'm gonna say "Enter your message: " and then I'm going to store that message in into message string,
and then we need to move that string into an array of bytes.
We're going to call that "buffer", and we want to get the bytes out of our string.
Next we're going to define our packet.
Again, it's going to be DatagramPacket.
We're going to name it "packet", and we're going to say - new DatagramPacket, and this time we're sending
>>buffer<<, where we have our message, >>buffer.length<<, the IP address -
we're going to use .getByName, and then instead of typing "localhost" - we can do that too - we're going
to type the IP address.
And then after that, we need the port number of the receiver.
And that's pretty much it.
That's our packet.
The only thing I'm missing is Shift + Ctrl + O.
Now we can just send our packet through the socket, and I'm going to print out a message saying -
all right.
That was the sending part.
Now we can focus on the receiving part.
I'm going to create a new array of bytes, since the previous one has been filled - so I'm gonna say new
byte[], and the length is going to be 1500 bytes.
And then I'm going to define a new packet -
the packet needs to have a buffer and we need to know what's the length.
And right after that,
I'm ready to receive my packet.
So I'm just gonna say .receive - and then we're going to store the packet that we received in to our packet object.
The only thing left to do now is to transfer the data from our buffer into our message string.
I'm gonna say >>buffer<< here, and then we need to use that trim() method, and then we can print out what we
received -
that should be it.
Let's save this and then try it out.
I'm going to run the receiver first and then I'm going to run the sender
I'm going to say "hi"
OK so I can see at the receiver that they have received my message.
I'm going to say "bye" - and this is it. Both the Receiver and the Sender are terminated, because we have reached
the end of our programs.
And as you can notice, we don't have the socket.close() here.
That's because we don't have a socket open and that is pretty much because in UDP, you don't have a connection
that is established before you send any data.
So in this program, we have pretty much sent each packet on its own, which is why the code looks like
this. In the next lesson,
I'm going to explain it even further, and we're going to add something to make this communication go
on.
So we want these two programs to be able to continue to communicate and we'll see how that's done in
the next lesson.