Solved

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

Posted on 2004-10-19
3
307 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:t_dot
3 Comments
 
LVL 2

Accepted Solution

by:
gen718 earned 75 total points
ID: 12352380
try using SO_REUSEADDR.

             socket = new DatagramSocket(null);              //  no bind will be done with this constructor
             socket.setReuseAddress(true);                      // must be done before bind call
             socket.bind(new InetSocketAddress(port));    // now bind specific address


Good Luck :)
0
 
LVL 9

Expert Comment

by:Ovi
ID: 12356801
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
0
 

Author Comment

by:t_dot
ID: 12357747
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now