Multicast receiver issue

Trustin Lee (이희승) trustin at gmail.com
Sat Nov 21 03:49:41 EST 2009


In your Netty code, you bound to the wrong port (0) while you bound to
the correct port (2222) in the old code.  After changing 0 to 2222, it
works fine for me.  Let me attach the working code for you.

— Trustin Lee, http://gleamynode.net/

On Wed, Nov 11, 2009 at 5:39 PM, Davide Rossoni
<rossoni.davide at gmail.com> wrote:
>
> Hello Trustin,
>     thanks for your prompt reply.
> This is the working receiver, hope this helps:
>
> public class OldMulticastReceiver {
>        private final Logger logger = LoggerFactory.getLogger(getClass());
>
>        private MulticastSocket mcastSocket;
>        private InetAddress mcastGroup;
>        private Listener listener;
>
>        public void joinGroup() throws IOException {
>                mcastGroup = InetAddress.getByName("228.10.10.10");
>                mcastSocket = new MulticastSocket(2222);
>                mcastSocket.setTimeToLive(1);
>
> mcastSocket.setNetworkInterface(NetworkInterface.getByInetAddress(InetAddress.getByName("172.26.16.20")));
>                mcastSocket.joinGroup(mcastGroup);
>
>                listener = new Listener();
>        }
>
>        public void start() throws IOException {
>                if (mcastSocket == null) {
>                        throw new IOException("Receiver could not be started: please join group
> first.");
>                }
>
>                Thread tWriter = new Thread(listener, "MulticastReceiver-Writer");
>                tWriter.start();
>        }
>
>        private class Listener implements Runnable {
>
>                private static final int DATAGRAM_BYTES = 128;
>                private boolean keepReceiving = true;
>
>                public void run() {
>                        int previousMessageID = -1;
>                        byte receive_buf[] = new byte[DATAGRAM_BYTES * 2];
>                        byte[] data;
>                        int len;
>
>                        ByteBuffer byteBuffer = ByteBuffer.allocate(DATAGRAM_BYTES);
>                        DatagramPacket mcastPacket = new DatagramPacket(receive_buf,
> receive_buf.length);
>
>                        while (keepReceiving) {
>                                try {
>                                        mcastPacket.setData(receive_buf, 0, receive_buf.length);
>
>                                        // Receive the datagram.
>                                        mcastSocket.receive(mcastPacket);
>
>                                        len = mcastPacket.getLength();
>                                        data = mcastPacket.getData();
>
>                                        logger.info("Here we are! Data = " + data + ", len = " + len);
>
>                                        byteBuffer.clear();
>                                        byteBuffer.put(data, 0, len);
>                                        byteBuffer.flip();
>
>                                } catch (IOException e) {
>                                        if (logger.isWarnEnabled()) logger.warn("Failed I/O: " +
> e.getMessage(), e);
>                                } catch (Exception e) {
>                                        logger.error("Error receiving message: " + e.getMessage(), e);
>                                }
>                        }
>                        try {
>                                mcastSocket.leaveGroup(mcastGroup);
>                        } catch (IOException e) {
>                                if (logger.isWarnEnabled()) logger.warn("Socket problem leaving group: "
> + e.getMessage(), e);
>                        }
>                        mcastSocket.close();
>
>                }
>        }
>
> }
>
>
>
>
>
> Trustin Lee wrote:
>>
>> Hello Davide,
>>
>> Thanks for reporting a problem first of all.
>>
>> This is weird.  Could you please post the working legacy code so that
>> I can find the difference?
>>
>> — Trustin Lee, http://gleamynode.net/
>>
>> _______________________________________________
>> netty-dev mailing list
>> netty-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/netty-dev
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Multicast-receiver-issue-tp3980856p3985023.html
> Sent from the Netty Developer Group mailing list archive at Nabble.com.
>
> _______________________________________________
> netty-dev mailing list
> netty-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-dev
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NewMulticastReceiver.java
Type: application/octet-stream
Size: 2909 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/netty-dev/attachments/20091121/75e32165/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OldMulticastReceiver.java
Type: application/octet-stream
Size: 3310 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/netty-dev/attachments/20091121/75e32165/attachment-0001.obj 


More information about the netty-dev mailing list