Question

I'm trying to create a simple multicast communication between my PC (Ubuntu, client) and my phone (Android, server).

Unicast/TCP connections work without any problem, the defined port (37659) opens both on PC and phone. When trying to use a MulticastSocket, no ports get opened. nmap tells me the specified port (36963) is a TCP port and that it is closed. (While the receive-method is being executed).

Am I doing something wrong? Or is the firewall blocking the multicast sockets? (I've tried about 20 different ports and none worked..., currently using port 36963)

EDIT: Also with the firewall completely down, nmap tells me the port is closed...

The server's code (phone):

private void multicastLoop() {
        String res = Build.FINGERPRINT + "\n";
        final InetAddress group;
        final MulticastSocket socket;
        final DatagramPacket response;
        try {
            group = InetAddress.getByName("224.0.0.115");
            socket = new MulticastSocket(mport);
            socket.setLoopbackMode(true);
            socket.setSoTimeout(10000);
            socket.joinGroup(group);
            response = new DatagramPacket(res.getBytes(), res.length(), group, mport);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                while(isRunning) {
                    try {
                        byte[] data = new byte[1024];
                        DatagramPacket dm = new DatagramPacket(data, data.length);
                        socket.receive(dm);
                        Log.d("udp", "received");
                        if (Arrays.equals(dm.getData(), "someone there".getBytes())) {
                            socket.send(response);
                        }
                    } catch (SocketTimeoutException e) {
                        continue;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    socket.leaveGroup(group);
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        });
        t.start();
    }

The client's code (computer):

public String[] findServers() {
        String hello = "someone there";
        try {
            InetAddress group = InetAddress.getByName(mhost);
            MulticastSocket socket = new MulticastSocket(mport);
            socket.setLoopbackMode(true);
            socket.setSoTimeout(60000);
            socket.joinGroup(group);
            DatagramPacket p = new DatagramPacket(hello.getBytes(), hello.length(), group, mport);
            byte[] buffer = new byte[1024];
            socket.send(p);
            DatagramPacket r = new DatagramPacket(buffer, buffer.length);
            socket.receive(r);
            socket.leaveGroup(group);
            socket.close();
            String srinfo = "";
            byte[] data = r.getData();
            for (byte b: data)
                srinfo += (char) b;
            System.out.println("Server found at " + r.getAddress().getHostName() + ": " + srinfo);
        } catch (SocketTimeoutException e) {
            return new String[] {"timeout"};
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
Was it helpful?

Solution

Make sure mhost is set to "224.0.0.115" not some machine name.

Make sure multicast is enabled on your router.

OTHER TIPS

  1. If the host is multi-homed, you need to join the multicast group via all local interfaces, not just the default one, which is what you're doing at present.

  2. You could send the response back to the source address it came from, which is in the received datagram packet. That would also mean that the client doesn't need a MulticastSocket, only a DatagramSocket.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top