gpt4 book ai didi

java - 实现一个持续向邻居广播的简单 UDP 网络

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:22:17 25 4
gpt4 key购买 nike

http://i.stack.imgur.com/97ERA.png

  1. 我有 3 个节点 A、B 和 C,它们具有各自的端口号。
  2. 我正在尝试编写一个接受3 个参数 的 java 程序:它的节点名称和它的2个相邻节点的端口并向它们广播一个字符串"Hello I'm A"(所以A会广播给B和 C)。它将每 3 秒执行一次。
  3. 该程序将在 3 个不同的实例中运行。
  4. 收到字符串后,它将打印从“Received string” 接收到的节点(端口 B 的示例)。

我很难实现这个,不过我听说过使用 UDP 的叫做 multicasting 的东西。这是我到目前为止的工作,我做错了什么?

class UDP {
public static void main(String[] args) throws Exception {
String nodeName = args[0];
int neighbourPort1 = Integer.valueOf(args[1]);
int neighbourPort2 = Integer.valueOf(args[2]);

while(true) {
Thread.sleep(3000); //every 3 seconds
//Continously broadcast and listen to neighbour1
DatagramSocket socket1 = null;
try {
//CREATE SOCKET TO NEIGHBOUR1
InetAddress host = InetAddress.getByName("localhost");
socket1 = new DatagramSocket();
socket1.connect(host, neighbour1);

//CREATE DATAGRAMS FOR SENDING
String message = "Hello I'm " + nodeName;
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, host, port);
socket1.send(sendPacket);

//CREATE DATAGRAMS FOR RECEIVING
byte[] receiveData = new byte[100]; //is there a way to determine the needed space?
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket1.receive(receivePacket);
System.out.println("Received string");

} catch(Exception e) { }
//Do the same for neighbour2, code is basically identical except for variables
DatagramSocket socket2 = null;
try {
//CREATE SOCKET TO NEIGHBOUR2
InetAddress host = InetAddress.getByName("localhost");
socket2 = new DatagramSocket();
socket2.connect(host, neighbour2);

//FOR SENDING DATAGRAMS
String message = "Hello I'm " + nodeName;
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, host, port);
socket2.send(sendPacket);

//FOR RECEIVING DATAGRAMS
byte[] receiveData = new byte[100]; //is there a way to determine the needed space?
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket2.receive(receivePacket);
System.out.println("Received string");

} catch(Exception e) { }
}

}
}

我知道我已经接近解决方案了。我能够正常广播,但让我兴奋的是不断收听的部分。

最佳答案

我认为最好使用一个单独的线程来监听自己端口上的数据。

  • A 将数据发送到 B 并阻塞,直到它从 B 收到数据包。
  • B 将数据发送到 C 并阻塞,直到它从 C 收到数据包。
  • C 将数据发送到 A 并阻塞,直到它从 A 收到数据包。

每个节点都在等待对方。只需发送数据包并等待 3 秒。另一个线程将只监听

public class UDP {

public static void main(String[] args) throws Exception {
final String nodeName = args[0];
final int ownPort = Integer.valueOf(args[1]);
final int neighbourPort1 = Integer.valueOf(args[2]);
final int neighbourPort2 = Integer.valueOf(args[3]);


// Don't create a new socket every time
DatagramSocket neighbour1 = new DatagramSocket();
DatagramSocket neighbour2 = new DatagramSocket();

neighbour1.connect(InetAddress.getLocalHost(), neighbourPort1);
neighbour2.connect(InetAddress.getLocalHost(), neighbourPort2);

// You have to LISTEN
new Thread() {
@Override
public void run() {
try {
DatagramSocket socket = new DatagramSocket(ownPort);

byte[] buffer = new byte[socket.getReceiveBufferSize()];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

while (true) {
// Blocks until it gets a packet
socket.receive(packet);

System.out.println("Received string");
}

// socket.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
}.start();

while (true) {
Thread.sleep(3000);

sendPacket(neighbour1, nodeName);
sendPacket(neighbour2, nodeName);
}

// If you're not using an infinite loop:
// neighbour1.close();
// neighbour2.close();
}

private static void sendPacket(DatagramSocket to, String from) throws Exception {
String message = "Hello I'm " + from;
byte[] data = message.getBytes();

DatagramPacket packet = new DatagramPacket(data, data.length);
to.send(packet);
}

}

关于java - 实现一个持续向邻居广播的简单 UDP 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com