Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 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

13 Experts available now in Live!

Get 1:1 Help Now