Week 89 — What is the purpose of the `DatagramSocket` class and how can it be used?

Question of the Week #89
What is the purpose of the DatagramSocket class and how can it be used?
3 Replies
dan1st | Daniel
dan1st | Daniel3mo ago
While Socket allows communicating via TCP, DatagramSocket can be used for sending and receiving UDP packets. It is possible to receive UDP packets by first creating a DatagramPacket object with a reference to a byte[] and the number of bytes it is allowed to load into that array and then calling DatagramSocket#receive(DatagramPacket) on the socket. Similarly, UDP packets can be sent by creating a DatagramPacket with the data to send and calling DatagramSocket#send(DatagramPacket) with it. For example, it is possible to create a server that is listening for UDP packets and always sending them back:
try(DatagramSocket sock = new DatagramSocket(1337)){
byte[] data = new byte[1024];
while(!Thread.currentThread().isInterrupted()){
//receive a packet
DatagramPacket resceivingPacket = new DatagramPacket(data, data.length);
sock.receive(resceivingPacket);
//send the same content back
DatagramPacket sendingPacket = new DatagramPacket(data, resceivingPacket.getLength(), resceivingPacket.getAddress(), resceivingPacket.getPort());
sock.send(sendingPacket);
}
}
try(DatagramSocket sock = new DatagramSocket(1337)){
byte[] data = new byte[1024];
while(!Thread.currentThread().isInterrupted()){
//receive a packet
DatagramPacket resceivingPacket = new DatagramPacket(data, data.length);
sock.receive(resceivingPacket);
//send the same content back
DatagramPacket sendingPacket = new DatagramPacket(data, resceivingPacket.getLength(), resceivingPacket.getAddress(), resceivingPacket.getPort());
sock.send(sendingPacket);
}
}
dan1st | Daniel
dan1st | Daniel3mo ago
It is then also possible to write a client that sends some sample data to the server and receives the response:
try(DatagramSocket sock = new DatagramSocket()){//if no port is specified, it will use use a random port
byte[] dataToSend = new byte[] {13, 37, 1, 2, 3, 123};
//send a packet to localhost on UDP port 1337
DatagramPacket sendingPacket = new DatagramPacket(dataToSend, dataToSend.length, InetAddress.getByName("localhost"), 1337);
sock.send(sendingPacket);
//receive a packet
byte[] receivedData = new byte[1024];
DatagramPacket resceivingPacket = new DatagramPacket(receivedData, receivedData.length);
sock.receive(resceivingPacket);
//print received data to console
System.out.println(Arrays.toString(Arrays.copyOf(receivedData, resceivingPacket.getLength())));//[13, 37, 1, 2, 3, 123]
}
try(DatagramSocket sock = new DatagramSocket()){//if no port is specified, it will use use a random port
byte[] dataToSend = new byte[] {13, 37, 1, 2, 3, 123};
//send a packet to localhost on UDP port 1337
DatagramPacket sendingPacket = new DatagramPacket(dataToSend, dataToSend.length, InetAddress.getByName("localhost"), 1337);
sock.send(sendingPacket);
//receive a packet
byte[] receivedData = new byte[1024];
DatagramPacket resceivingPacket = new DatagramPacket(receivedData, receivedData.length);
sock.receive(resceivingPacket);
//print received data to console
System.out.println(Arrays.toString(Arrays.copyOf(receivedData, resceivingPacket.getLength())));//[13, 37, 1, 2, 3, 123]
}
📖 Sample answer from dan1st
dan1st | Daniel
dan1st | Daniel3mo ago
Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming using the UDP instead of TCP.
Submission from mitsuki087248
Want results from more Discord servers?
Add your server