Reputation:
I am being sent an xml feed via multicast, but I don't know the multicast group address. Can I just use localhost instead?
Socket socket =
new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPEndPoint ip = new IPEndPoint(IPAddress.Any,8888);
socket.Bind(ip);
socket.SetSocketOption
(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse("127.0.0.1"),IPAddress.Any));
byte[] data = new byte[1024];
int length = socket.Receive(data);
...
Upvotes: 2
Views: 716
Reputation: 1632
Strictly speaking, if you open a listening port for multicast data on a non-multicast address, then you are essentially listening to standard UDP. The difference between multicast and UDP is in the IP address. Its a special IPV4 address range that is not tied to a fixed host. Rather it is recognized by the routers on the edges of your network in a pseudo publish-subscribe fashion. Within your sub-net multicast is for all intents and purposes the same as broadcast.
If you write to a multicast address, it is available to all hosts on your subnet. If your router supports multicast, then it will provide it upstream to any clients that announce that they are interested in it. THink of it as publish/subscribe for subnets.
All of this to say, if you are looking for a localhost equivalent to multicast, then you probably need to look at broadcast instead.
Upvotes: 2
Reputation: 175593
No.
You (your client) needs to join the multicast group, you'll AddMembership to the multicast group IP, then connect.
Otherwise you won't be able to receive the multicast feed. Your code would work with a UDP broadcast though.
Upvotes: 2