diff --git a/network/src/main/java/kr/pe/elex/examples/multicast/MulticastUdpClientSample.java b/network/src/main/java/kr/pe/elex/examples/multicast/MulticastUdpClientSample.java index 4f374a2..6ec7cb3 100644 --- a/network/src/main/java/kr/pe/elex/examples/multicast/MulticastUdpClientSample.java +++ b/network/src/main/java/kr/pe/elex/examples/multicast/MulticastUdpClientSample.java @@ -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); - // 수신 - 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); + 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: {}", name, received); + } socket.leaveGroup(group); socket.close();