Java中的多播无法正常工作

时间:2012-02-05 16:25:59

标签: java networking multicast

我正在为我的班级开发一些同行2对等应用程序,我被告知让服务器发现彼此必须多播到他们的UDP端口1110并听取他们的UDP端口1110.我写了如下代码这样做。为了测试,我运行了两个发送和接收的服务器。但似乎没什么用。你认为我的问题在哪里?

我将2台服务器放在2个不同的文件夹中。我分配到我的网卡的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 "";
    }
}

1 个答案:

答案 0 :(得分:0)

您需要尝试广播地址192.168.0.255

另一种方法是使用多播而不是广播地址,如224.x.x.x,它不与特定子网绑定。