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