mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yigal Rachman <yi...@uvic.ca>
Subject Re: How to receive UDP broadcast datagrams?
Date Thu, 28 Feb 2008 19:52:22 GMT
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
>


Mime
View raw message