You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Yigal Rachman <yi...@uvic.ca> on 2008/02/28 19:21:11 UTC

How to receive UDP broadcast datagrams?

Hi, Folks:

I have tried the MINA 2.0 UDP example - it works great!

As luck would have it, my UDP application is different from the 
example.  I need to receive UDP datagrams that are being broadcast on 
the subnet by an existing system.  I have researched this on the Web and 
tried a number of different ways to do it, but have not yet figured it 
out.  In particular, I do not know how to set up a DataGramAcceptor to 
do this.

I would welcome any help / suggestions that you may have.

Thank you,
Yigal Rachman

Re: How to receive UDP broadcast datagrams?

Posted by Yigal Rachman <yi...@uvic.ca>.
Hi, folks:

Ok - I figured it out after some digging.  The secret is to bind the 
acceptor to just a port - let the system pick the ip address.  This uses 
the "wildcard" ip address which will receive any datagrams broadcast on 
the port.

I found this in the most obvious place of all (yes - it is always in the 
last place you look..) - the javadoc for DatagramSocket: 
http://java.sun.com/j2se/1.5.0/docs/api/java/net/DatagramSocket.html

So the Mina code looks something like this (so easy once you know how):

       int ipPort = 9999;

        // motherhood stuff - just a test IoHandler
        DatagramAcceptor acceptor = new NioDatagramAcceptor();
        acc.setHandler(new IoHandlerAdapter() {
            public void messageReceived(IoSession session, Object obj)
            throws Exception {
                log.debug("received " + obj + " on " + 
session.getLocalAddress());
            }
        });

        // the magic ingredient - set *just the port*
        log.debug("binding listener to wildcard address + port " + ipPort);
        acceptor.bind(new InetSocketAddress(ipPort));

Hope this is of use to someone.

Thanks again for a super framework.
Yigal Rachman


Yigal Rachman wrote:
> Hi, Folks:
>
> I have tried the MINA 2.0 UDP example - it works great!
>
> As luck would have it, my UDP application is different from the 
> example.  I need to receive UDP datagrams that are being broadcast on 
> the subnet by an existing system.  I have researched this on the Web 
> and tried a number of different ways to do it, but have not yet 
> figured it out.  In particular, I do not know how to set up a 
> DataGramAcceptor to do this.
>
> I would welcome any help / suggestions that you may have.
>
> Thank you,
> Yigal Rachman
>