2021-08-11
This commit is contained in:
@@ -11,24 +11,48 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Slf4j
|
||||
public class MulticastUdpClientSample {
|
||||
public static void main(String... args) throws IOException {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
client("ONE");
|
||||
} catch (IOException e) {
|
||||
log.error("Oops", e);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
client("TWO");
|
||||
} catch (IOException e) {
|
||||
log.error("Oops", e);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public static void client(String name) throws IOException {
|
||||
MulticastSocket socket = new MulticastSocket(18888);
|
||||
InetAddress group = InetAddress.getByName("224.0.0.1");
|
||||
socket.joinGroup(group);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// 수신
|
||||
byte[] buf = new byte[256];
|
||||
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||
socket.receive(packet);
|
||||
String received = new String(packet.getData(), 0, packet.getLength());
|
||||
log.info("Rx: {}", received);
|
||||
log.info("{} Rx: {}", name, received);
|
||||
}
|
||||
|
||||
socket.leaveGroup(group);
|
||||
socket.close();
|
||||
|
||||
Reference in New Issue
Block a user