Java 中的多播不起作用

发布于 2025-01-02 06:15:41 字数 3476 浏览 3 评论 0原文

我正在为我的班级开发一些对等 2 对等应用程序,有人告诉我要让服务器发现彼此,他们必须多播到他们的 UDP 端口 1110 并监听他们的 UDP 端口 1110。我编写了如下代码来执行此操作。为了进行测试,我运行了两台服务器,它们既发送又接收。但似乎没有什么作用。你认为我的问题出在哪里?

我将两台服务器放在两个不同的文件夹中。我为我的网卡分配了 IP 地址,如下所示 ifconfig eth0:3 192.168.0.11 netmask 255.255.255.0 up 我应该如何告诉每个服务器新的 IP 地址?

BroadcastListner

class BroadcastListner implements Callable<Object> {
    int PORT = 1110;
    String IP = "255.255.255.255";
    MulticastSocket socket ; 
    DatagramPacket packet;
    InetAddress IPAD; 
    byte data[] =  null ;  //////////////change size
    int numOfNodes;

    BroadcastListner(String IP, int numOfNodes) {
        try {
            this.numOfNodes = numOfNodes;
        this.IP = IP;
        IPAD = InetAddress.getByName(IP);
        socket = new MulticastSocket(PORT);
        packet = new DatagramPacket(data,data.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    BroadcastListner(int numOfNodes) {
        try{
            this.numOfNodes = numOfNodes;
            // this.IP = IP;
            IPAD = InetAddress.getByName(IP);
            socket = new MulticastSocket(PORT);
            packet = new DatagramPacket(data,data.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String call() {
        try{
            socket.joinGroup(IPAD);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        while(true) {
            try {
                socket.receive(packet); 
                String str = new String(packet.getData());
                System.out.println(" Time signal received from"+
                packet.getAddress() + " Time is : " +str);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }

        //socket.leaveGroup(IPAD);
        //socket.close();
        //return "";
    }
}

BroadcastSender

class BroadcastSender implements Callable<Object> {
    int PORT = 1110;
    String IP = "255.255.255.255";
    MulticastSocket socket; 
    DatagramPacket packet;
    InetAddress IPAD; 
    byte[] data = "IAmAServer".getBytes(); 
    //int numOfNodes;
    String str = "IAmAServer";

    BroadcastSender(String IP) {
        try {
            // this.numOfNodes = numOfNodes;
            this.IP = IP;
            IPAD = InetAddress.getByName(IP);
            socket = new MulticastSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    BroadcastSender() {
        try{
            // this.numOfNodes = numOfNodes;
            // this.IP = IP;
            IPAD = InetAddress.getByName(IP);
            socket = new MulticastSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String call() {
        try {
            socket.joinGroup(IPAD);
            socket.setTimeToLive(10);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        while(true) {
            try {
                Thread.sleep(2000);
                packet = new DatagramPacket (data,str.length(),IPAD,PORT);
                socket.send(packet);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }
        //return "";
    }
}

I'm developing some peer 2 peer app for my class and I was told for letting servers to discover each other they have to Multicast to their UDP port 1110 and listen to their UDP port 1110. I wrote the code like below for doing that. for testing I run 2 servers which both send and receive. but it seems nothing working. where do you think my problem is ?

I put 2 servers in 2 different folders. and I assigned to IP addresses to my NIC like this ifconfig eth0:3 192.168.0.11 netmask 255.255.255.0 up how should I tell each server about the new ip address?

BroadcastListner

class BroadcastListner implements Callable<Object> {
    int PORT = 1110;
    String IP = "255.255.255.255";
    MulticastSocket socket ; 
    DatagramPacket packet;
    InetAddress IPAD; 
    byte data[] =  null ;  //////////////change size
    int numOfNodes;

    BroadcastListner(String IP, int numOfNodes) {
        try {
            this.numOfNodes = numOfNodes;
        this.IP = IP;
        IPAD = InetAddress.getByName(IP);
        socket = new MulticastSocket(PORT);
        packet = new DatagramPacket(data,data.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    BroadcastListner(int numOfNodes) {
        try{
            this.numOfNodes = numOfNodes;
            // this.IP = IP;
            IPAD = InetAddress.getByName(IP);
            socket = new MulticastSocket(PORT);
            packet = new DatagramPacket(data,data.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String call() {
        try{
            socket.joinGroup(IPAD);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        while(true) {
            try {
                socket.receive(packet); 
                String str = new String(packet.getData());
                System.out.println(" Time signal received from"+
                packet.getAddress() + " Time is : " +str);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }

        //socket.leaveGroup(IPAD);
        //socket.close();
        //return "";
    }
}

BroadcastSender

class BroadcastSender implements Callable<Object> {
    int PORT = 1110;
    String IP = "255.255.255.255";
    MulticastSocket socket; 
    DatagramPacket packet;
    InetAddress IPAD; 
    byte[] data = "IAmAServer".getBytes(); 
    //int numOfNodes;
    String str = "IAmAServer";

    BroadcastSender(String IP) {
        try {
            // this.numOfNodes = numOfNodes;
            this.IP = IP;
            IPAD = InetAddress.getByName(IP);
            socket = new MulticastSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    BroadcastSender() {
        try{
            // this.numOfNodes = numOfNodes;
            // this.IP = IP;
            IPAD = InetAddress.getByName(IP);
            socket = new MulticastSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String call() {
        try {
            socket.joinGroup(IPAD);
            socket.setTimeToLive(10);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        while(true) {
            try {
                Thread.sleep(2000);
                packet = new DatagramPacket (data,str.length(),IPAD,PORT);
                socket.send(packet);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }
        //return "";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2025-01-09 06:15:41

您需要尝试 192.168.0.255 的广播地址。

另一种方法是使用多播而不是像 224.xxx 这样不与特定子网绑定的广播地址。

You need to try the broadcast address of 192.168.0.255

An alternative is to use a multi-cast instead of broadcast address like 224.x.x.x which is not tied to a specific subnet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文