Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
servlet  URL Rewriting 1 42
how to see all occupied ports on windows 10 laptop 15 77
going to wrong jsp page 2 34
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

820 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