Solved

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

Posted on 2004-10-19
3
308 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 191
Java Server Faces parameter pass? 6 48
servlet requestdispatcher include and forward differences 1 30
ForLoop Example 3 47
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

815 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

7 Experts available now in Live!

Get 1:1 Help Now