4

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 "";
    }
}
beerbajay
  • 19,652
  • 6
  • 58
  • 75
ePezhman
  • 4,010
  • 7
  • 44
  • 80
  • not certain if there are other problems but I think your IP is wrong. There is a range of addresses reserved for multicast. You need to pick one of them. For example, 224.x.x.x. – Kevin Feb 05 '12 at 16:30
  • To reiterate what @Kevin posted, you should not be using your network mask as the IP address. – Perception Feb 05 '12 at 16:56

1 Answers1

0

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I haven't seen using a broadcast address before. Is that the same as using a multicast address or is it restricted to the 192.168.0.x subnet? – Kevin Feb 05 '12 at 23:02