Skip to content

Commit f76913f

Browse files
committed
BAEL-4786 - DatagramChannel - review fix
1 parent 20775ad commit f76913f

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.datagramchannel;
2+
3+
import java.io.IOException;
4+
import java.net.SocketAddress;
5+
import java.nio.channels.DatagramChannel;
6+
7+
public class DatagramChannelBuilder {
8+
9+
public static DatagramChannel openChannel() throws IOException {
10+
DatagramChannel datagramChannel = DatagramChannel.open();
11+
return datagramChannel;
12+
}
13+
14+
public static DatagramChannel bindChannel(SocketAddress local) throws IOException {
15+
return openChannel().bind(local);
16+
}
17+
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.datagramchannel;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.DatagramChannel;
8+
9+
public class DatagramClient {
10+
11+
public static DatagramChannel startClient() throws IOException {
12+
DatagramChannel client = DatagramChannelBuilder.bindChannel(null);
13+
client.configureBlocking(false);
14+
return client;
15+
}
16+
17+
public static void sendMessage(DatagramChannel client, String msg, SocketAddress serverAddress) throws IOException {
18+
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
19+
client.send(buffer, serverAddress);
20+
client.close();
21+
}
22+
23+
public static void main(String[] args) throws IOException {
24+
DatagramChannel client = startClient();
25+
String msg = "Hello, this is a Baeldung's DatagramChannel based UDP client!";
26+
InetSocketAddress serverAddress = new InetSocketAddress("localhost", 7001);
27+
28+
sendMessage(client, msg, serverAddress);
29+
30+
}
31+
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.datagramchannel;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.DatagramChannel;
8+
9+
public class DatagramServer {
10+
11+
public static DatagramChannel startServer() throws IOException {
12+
InetSocketAddress address = new InetSocketAddress("localhost", 7001);
13+
DatagramChannel server = DatagramChannelBuilder.bindChannel(address);
14+
15+
System.out.println("Server started at #" + address);
16+
17+
return server;
18+
}
19+
20+
public static String receiveMessage(DatagramChannel server) throws IOException {
21+
ByteBuffer buffer = ByteBuffer.allocate(1024);
22+
SocketAddress remoteAdd = server.receive(buffer);
23+
String message = extractMessage(buffer);
24+
25+
System.out.println("Client at #" + remoteAdd + " sent: " + message);
26+
27+
server.close();
28+
29+
return message;
30+
}
31+
32+
private static String extractMessage(ByteBuffer buffer) {
33+
buffer.flip();
34+
35+
byte[] bytes = new byte[buffer.remaining()];
36+
buffer.get(bytes);
37+
38+
String msg = new String(bytes);
39+
40+
return msg;
41+
}
42+
43+
public static void main(String[] args) throws IOException {
44+
DatagramChannel server = startServer();
45+
receiveMessage(server);
46+
}
47+
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.datagramchannel;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.net.InetSocketAddress;
7+
import java.nio.channels.DatagramChannel;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class DatagramChannelUnitTest {
11+
12+
@Test
13+
public void whenClientSendsAndServerReceivesUDPPacket_thenCorrect() throws IOException {
14+
DatagramChannel server = DatagramServer.startServer();
15+
16+
DatagramChannel client = DatagramClient.startClient();
17+
String msg = "Hello, this is a Baeldung's DatagramChannel based UDP client!";
18+
InetSocketAddress serverAddress = new InetSocketAddress("localhost", 7001);
19+
DatagramClient.sendMessage(client, msg, serverAddress);
20+
21+
assertEquals("Hello, this is a Baeldung's DatagramChannel based UDP client!", DatagramServer.receiveMessage(server));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)