From beeb2b0e1ade870cfeaf8d8c2ed78a5e53a22fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= <1101766085@qq.com> Date: Mon, 23 Aug 2021 18:53:14 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=80=83=E8=99=91=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20udp=20=E5=A4=9A=E6=92=AD=E5=81=9A=E9=9B=86=E7=BE=A4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/server/cluster/package-info.java | 1 + .../net/dreamlu/iot/mqtt/core/udp/Node1.java | 42 ++++++++++++++ .../net/dreamlu/iot/mqtt/core/udp/Node2.java | 56 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/cluster/package-info.java create mode 100644 mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node1.java create mode 100644 mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node2.java diff --git a/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/cluster/package-info.java b/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/cluster/package-info.java new file mode 100644 index 0000000..f64c4bb --- /dev/null +++ b/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/cluster/package-info.java @@ -0,0 +1 @@ +package net.dreamlu.iot.mqtt.core.server.cluster; diff --git a/mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node1.java b/mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node1.java new file mode 100644 index 0000000..1e0b717 --- /dev/null +++ b/mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node1.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.core.udp; + +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; + +public class Node1 { + public static void main(String[] args) throws Exception { + int port = 12345; + // 多播 ip 段 224.0.0.0 to 239.255.255.255 + MulticastSocket multicastSocket = new MulticastSocket(port); + InetAddress group = InetAddress.getByName("224.0.0.1"); + multicastSocket.joinGroup(group); + System.out.println("Multicast Receiver running at:" + multicastSocket.getLocalSocketAddress()); + + while (true) { + DatagramPacket dp = new DatagramPacket(new byte[1024], 1024); + multicastSocket.receive(dp); + String s = new String(dp.getData(), 0, dp.getLength()); + System.out.println("receive: " + s); + } + +// mcSocket.leaveGroup(group); +// mcSocket.close(); + } +} diff --git a/mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node2.java b/mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node2.java new file mode 100644 index 0000000..94b3ebe --- /dev/null +++ b/mica-mqtt-core/src/test/java/net/dreamlu/iot/mqtt/core/udp/Node2.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.core.udp; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.util.Timer; +import java.util.TimerTask; + +public class Node2 { + public static void main(String[] args) throws Exception { + int port = 12345; + // 多播 ip 段 224.0.0.0 to 239.255.255.255 + MulticastSocket multicastSocket = new MulticastSocket(port); + InetAddress group = InetAddress.getByName("224.0.0.1"); + multicastSocket.joinGroup(group); + + System.out.println("Multicast Receiver running at:" + multicastSocket.getLocalSocketAddress()); + + String message = "Hello from node2"; + byte[] buffer = message.getBytes(); + + TimerTask timerTask = new TimerTask() { + @Override + public void run() { + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port); + try { + multicastSocket.send(packet); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + + Timer timer = new Timer(); + timer.schedule(timerTask, 1000, 1000); +// mcSocket.leaveGroup(group); +// mcSocket.close(); + } +} -- GitLab