Link to home
Start Free TrialLog in
Avatar of t_dot
t_dot

asked on

Java: Need to have TWO sockets listening to UDP broadcasts on the same port, how?

I am NOT a Java newbie, but this has stumped me.

DO NOT ATTEMPT TO TELL ME I SHOULD BE DOING SOMETHING ELSE -- I don't have the time to explain why I need to do what i'm asking, but trust me, I need to.

I need to be able to have two DatagramSockets listening to UDP broadcasts on the **SAME PORT**, on the same machine. However, this just doesn't seem possible (can only bind one). How can it be done?

For instance, if I try and have some relaying going on, it still doesn't work: as soon as you bind something to wildcard port X (0.0.0.0 port X), you can't even then bind something to a specific IP, port X ! (ie. 0.0.0.0 port X and 10.101.1.5 port X).  This rules out having the first person to bind to 0.0.0.0 port X then passing on the packet to the other processes (they'd all have to be on a different port, bah, confusing)

Thanks,

Tim
ASKER CERTIFIED SOLUTION
Avatar of gen718
gen718

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ovi
Ovi

What you are trying to do (I hope) is multicasting. For non-blocking sockets which are using the same port to listen/emit output, the simplest way I know is to use MulticastSocket.

Some examples of using multicast sockets, you can find at the bottom of this page:
http://javaalmanac.com/egs/java.net/pkg.html#Multicast

Exact links:
http://javaalmanac.com/egs/java.net/JoinMulticastGroup.html
http://javaalmanac.com/egs/java.net/ReadMulticast.html
http://javaalmanac.com/egs/java.net/WriteMulticast.html

If you need more help, feel free to ask.
Ovi
Avatar of t_dot

ASKER

Yeah, thanks gen718 that fixes it. I can't *believe* I didn't think of that. Doh! I guess I was pretty tired :)

Thanks again!

tim