From ac0327fcfa3df4b85ba00425b38e17d3c93c7976 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 3 Apr 2021 13:55:55 +0300 Subject: [PATCH] BAEL-4786 - DatagramChannel --- .../DatagramChannelBuilder.java | 18 +++++++ .../datagramchannel/DatagramClient.java | 32 ++++++++++++ .../datagramchannel/DatagramServer.java | 49 +++++++++++++++++++ .../DatagramChannelUnitTest.java | 24 +++++++++ 4 files changed, 123 insertions(+) create mode 100644 core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramChannelBuilder.java create mode 100644 core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramClient.java create mode 100644 core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramServer.java create mode 100644 core-java-modules/core-java-nio-2/src/test/java/com/baeldung/datagramchannel/DatagramChannelUnitTest.java diff --git a/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramChannelBuilder.java b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramChannelBuilder.java new file mode 100644 index 0000000000..43e0747622 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramChannelBuilder.java @@ -0,0 +1,18 @@ +package com.baeldung.datagramchannel; + +import java.io.IOException; +import java.net.SocketAddress; +import java.nio.channels.DatagramChannel; + +public class DatagramChannelBuilder { + + public static DatagramChannel openChannel() throws IOException { + DatagramChannel datagramChannel = DatagramChannel.open(); + return datagramChannel; + } + + public static DatagramChannel bindChannel(SocketAddress local) throws IOException { + return openChannel().bind(local); + } + +} diff --git a/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramClient.java b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramClient.java new file mode 100644 index 0000000000..867378cc50 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramClient.java @@ -0,0 +1,32 @@ +package com.baeldung.datagramchannel; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; + +public class DatagramClient { + + public static DatagramChannel startClient() throws IOException { + DatagramChannel client = DatagramChannelBuilder.bindChannel(null); + client.configureBlocking(false); + return client; + } + + public static void sendMessage(DatagramChannel client, String msg, SocketAddress serverAddress) throws IOException { + ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); + client.send(buffer, serverAddress); + client.close(); + } + + public static void main(String[] args) throws IOException { + DatagramChannel client = startClient(); + String msg = "Hello, this is a Baeldung's DatagramChannel based UDP client!"; + InetSocketAddress serverAddress = new InetSocketAddress("localhost", 7001); + + sendMessage(client, msg, serverAddress); + + } + +} diff --git a/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramServer.java b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramServer.java new file mode 100644 index 0000000000..219531209b --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/datagramchannel/DatagramServer.java @@ -0,0 +1,49 @@ +package com.baeldung.datagramchannel; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; + +public class DatagramServer { + + public static DatagramChannel startServer() throws IOException { + InetSocketAddress address = new InetSocketAddress("localhost", 7001); + DatagramChannel server = DatagramChannelBuilder.bindChannel(address); + server.configureBlocking(false); + + System.out.println("Server started at #" + address); + + return server; + } + + public static String receiveMessage(DatagramChannel server) throws IOException { + ByteBuffer buffer = ByteBuffer.allocate(1024); + SocketAddress remoteAdd = server.receive(buffer); + String message = extractMessage(buffer); + + System.out.println("Client at #" + remoteAdd + " sent: " + message); + + server.close(); + + return message; + } + + private static String extractMessage(ByteBuffer buffer) { + buffer.flip(); + + byte bytes[] = new byte[buffer.remaining()]; + buffer.get(bytes); + + String msg = new String(bytes); + + return msg; + } + + public static void main(String[] args) throws IOException { + DatagramChannel server = startServer(); + receiveMessage(server); + } + +} diff --git a/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/datagramchannel/DatagramChannelUnitTest.java b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/datagramchannel/DatagramChannelUnitTest.java new file mode 100644 index 0000000000..b971cc55f5 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/datagramchannel/DatagramChannelUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.datagramchannel; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.DatagramChannel; +import org.junit.jupiter.api.Test; + +public class DatagramChannelUnitTest { + + @Test + public void whenClientSendsAndServerReceivesUDPPacket_thenCorrect() throws IOException { + DatagramChannel server = DatagramServer.startServer(); + + DatagramChannel client = DatagramClient.startClient(); + String msg = "Hello, this is a Baeldung's DatagramChannel based UDP client!"; + InetSocketAddress serverAddress = new InetSocketAddress("localhost", 7001); + DatagramClient.sendMessage(client, msg, serverAddress); + + assertEquals("Hello, this is a Baeldung's DatagramChannel based UDP client!", DatagramServer.receiveMessage(server)); + } + +}