简体   繁体   中英

multicast feed

I'm being sent an xml feed via multicast, but don't know the multicast group address. Can I just use localhost instead; that is,

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);

...

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.

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.

与单播不同,您确实需要知道多播的组地址。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM