The following is the listen function I used to receive multicast
packets.
"socket.receive" can not receive packets from remote peer, but it can
receive packets sent from local.


    public static void listen() throws IOException {
        Runnable r = new Runnable() {
            public void run() {

                while (true) {
                    try {
                        MulticastSocket socket = new MulticastSocket
(1900);
                        Log.v(TAG, "Setting multicast network
interface: " + ni);
                        socket.setNetworkInterface(ni);

                        socket.setTimeToLive(4);
                        socket.setReuseAddress(true);
                        socket.joinGroup(getMulticastAddress());
                        while(true)
                        {
                            byte[] buf = new byte[1024];
                            DatagramPacket packet_r = new
DatagramPacket(buf, buf.length);
                            socket.receive(packet_r);
                            String s = new String(packet_r.getData());

                            Log.v(TAG, "Receiving a Multicast from ["
+ packet_r.getAddress().getHostAddress() + ":" + packet_r.getPort() +
"]");
                            Log.v(TAG, "Data: " + s);

                            CharSequence data = packet_r.getAddress
().getHostAddress() + ":" + packet_r.getPort();
                            Toast.makeText(view, data,
Toast.LENGTH_SHORT).show();
                        }
                    } catch (IOException e) {
                        Log.v(TAG, "UPNP network exception", e);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {}
                    }
                }

            }};
        sListener = new Thread(r);
        sListener.start();
    }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to