1

I have a very similar behaviour as described here:

  • running on a Mac Book Pro, Snow Leopard
  • using Multicast Sockets to send and receive packets on localhost

I'm using Eclipse and observed the following behaviour when the client / server are started from within the workspace:

  • if the wireless interface (airport) is up and running, the client does not receive any packets
  • if the interface is turned off, everything works as expected

But what I don't understand is:

  • if I create a JAR and run the code in any console -> all good! Just Eclipse seems not to like airport ;-)
  • depending on what wireless network I'm connected to, the above behaviour might change, i.e. it also works if airport is enabled (for example @ Uni)

Does anyone have an idea 'bout this?

Cheers

Below the straightforward code for server / client:

@Override
public void run() {
    String multicastAddress = "224.0.0.2";
    int multicastPort = 8000;
    MulticastSocket socket = null;
    try {
        try {
            InetAddress multicastGoup = InetAddress.getByName(multicastAddress);
            socket = new MulticastSocket(multicastPort);
            socket.joinGroup(multicastGoup);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        byte[] buffer = new byte[1024];

        while (true) {
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            System.out.println("BEFORE RECEIVE: listening on " + multicastAddress + ":" + multicastPort);
            socket.receive(packet);
            System.out.println("PACKET RECEIVED");

            System.err.println("Client received: " + new String(packet.getData()));
        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        socket.close();
    }

}

Server:

    public void run() {
    MulticastSocket socket = null;
    try {
        String multicastAddress = "224.0.0.2";
        int multicastPort = 8000;
        InetAddress multicastGoup = InetAddress.getByName(multicastAddress );
        socket = new MulticastSocket(multicastPort);
        socket.joinGroup(multicastGoup);

        byte[] data = new String("Teststring").getBytes();

        while (true) {
            socket.send(new DatagramPacket(data, data.length, multicastGoup, multicastPort));
            System.out.println("SERVER: Datagram sent");
            Thread.sleep(1000);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        socket.close();
    }
}
Community
  • 1
  • 1
Christof
  • 779
  • 8
  • 21
  • I think this problem is related to what interface the `joinGroup()` method chooses as default - and this (the default) varies according to Eclipse settings and available intefaces). Can you select an inteface to use when calling `joinGroup()` and see if the problem continues? – ypercubeᵀᴹ Mar 23 '11 at 06:32

1 Answers1

1

From Class MulticastSocket:

void  joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
          Joins the specified multicast group at the specified interface.

Try using a specific interface so your joinGroup doesn't fall into the default - which may vary according on available, open ones or due to Eclipse settings.

joinGroup

public void joinGroup(SocketAddress mcastaddr,
                      NetworkInterface netIf)
               throws IOException

  Joins the specified multicast group at the specified interface.

  If there is a security manager, this method first calls its
  checkMulticast method with the mcastaddr argument as its argument.

  Parameters:
    mcastaddr - is the multicast address to join
    netIf - specifies the local interface to receive
        multicast datagram packets,
      -- here is the catch
      or null to defer to the interface set by
        setInterface(InetAddress) or 
        setNetworkInterface(NetworkInterface) 
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235