Trying to get friendly peers
I am spending way too much time on this stupid feature, so I might as well ask for help. The situation is simple, I am trying to rewrite the DHT implementation to configure itself dynamically and on the fly. That requires each DHT node to be able to publish its presence on the network.
It looks simple, all you have to do is a UDP multicast over the network and then the real complexity starts (how to negotiate which node does what). The problem is that I can't get it to work in a way that would allow two nodes to run on the same machine.
Now, I am pretty sure that I am missing something huge here, but this is pretty much my first foray into the world of UDP, and most of my knowledge is theoretical. What I am trying to do is to multicast a "hello, I am here" message, which can be picked up by any listening node on the network, including nodes that are running on the current machine.
This is where I am running into the problem, I can't figure out how to get two apps listening to the same port, or, how to send the multicast message to multiple ports.
Note that I am using UDP here because it is the thing that should gives me this facility with the least dependencies possible. I tried using System.Net.PeerToPeer, but I wasn't able to get it to give me a list of peers and join notifications.
Any ideas?
Comments
Does this help any? .SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
Found at: bytes.com/.../233554-udp-listen-same-port-two-a...
~Lee
I don't know if there are any DBs involved but you could register each node in a table or some other common data source. But perhaps that is what you are trying to avoid with the dependency comment, just thought I would mention it.
I cant figure out if your problem is hard or easy, but just in case its the easy one:
You can not have two applications bound to the same IP address and port.
You can have two applications bound to the same port if they are bound to different IP addresses, so start by giving your PC a second IP address and go from there.
Check out lines 171 in Transports/MulticastUdpEndpoint in MassTransit trunk. The InitializeReceiver method does just what you want.
In fact, there is a unit test with two buses on the same multicast endpoint getting all the business.
This works with two clients on the same machine joined to the same group address.
Have you considered Zeroconf/Bonjour at all?
I think you'd either need a server that allows peers to register themselves and gives that list to other peers requesting it (and updates when new nodes register) or you'd need a way scan your network. You get an IP address just fire off packets to all other IP addresses at the pre-determined port, if they respond then you know they exist.
You should have a look at WS-Discovery a standard for exactly that.
You can find a sample implementation in C# on:
blogs.msdn.com/.../...y-sample-implementation.aspx
Bo,
It looks like multicast UDP is much simpler
Michael,
That is additional dependency
Chris,
Thanks, exactly what I need.
Lee,
Thanks!
You might want to look at WS-Discovery a little more. For instance it's an inherent facility of WCF in .Net 4.0
weblogs.asp.net/.../...s-discovery-in-wcf-4-0.aspx
Probably a little heavier than you had in mind and they're a Win7 bits...
blogs.msdn.com/.../...ble-with-the-drt-part-1.aspx
blogs.msdn.com/.../...ting-table-in-windows-7.aspx
Comment preview