因此,我正在为我的课程做一个分布式系统,我需要实现一个MulticastServer来这样做。 事情是,我正在尝试在服务器上接收消息,但是我得到了重复的数据包,因此有两条相同的消息... 我目前在Mac上,并且尝试使用Terminal和Intel Ij,但是当我在朋友的PC中尝试时,绝对没有问题。
import java.net.MulticastSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.BufferedInputStream;
import java.util.HashMap;
public class MulticastServer extends Thread {
private String MULTICAST_ADDRESS = "224.0.224.0";
private int PORT_ENV = 4321; // Porto para enviar
private int PORT_REC = 4322; // Porto para receber
public static void main(String[] args) {
MulticastServer server = new MulticastServer();
server.start();
}
public MulticastServer() {
super("Server running " + (long) (Math.random() * 1000));
}
public void run() {
MulticastSocket socket = null;
System.out.println(this.getName());
try {
socket = new MulticastSocket(PORT_ENV);
InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
socket.joinGroup(group);
while(true) {
byte[] buffer = new byte[256];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
System.out.println("Received packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + " with message:");
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
socket.close();
}
}
请帮帮我:)