简体   繁体   中英

How can I know number of group members in multicast UDP?

I'm making chat program using C language. In there, I have to know number of group members using same multicast address. Is there any way to know that?

In general, there's no built-in way to know how many members there are. There could in theory be thousands of them.

You could use a pcap library to listen for IGMP messages, but that will only tell you when someone joins or leaves the group, not who's currently in the group. You could also ping the multicast group and see who responds, but you'll need a pcap library for that as well.

In the context of your application, you probably want to know how many instances of the app are running. In that case, you need to define a particular type of message in your protocol that acts like a ping. One instance of the app would send this message when it starts up, and any other instance that receives it should respond.

There could be scalability issues with a large number of members, but if you're just starting out it should work fine.

You can't do it by snooping IGMP.

This information is suppressed by every host and router between you and the clients, including the client hosts themselves. The IGMP join and leave messages are only sent when the number of members known to the sender goes to one or zero respectively. If another member joins in the same host, another join will not be sent. Similarly, if a join message reaching a router makes the number of member hosts known to the router go from zero to one, it sends an upstream join, but not when it goes from one to two or more.

The upshot is that all that any specific host knows is that there is or isn't a downstream member.

If you want to know the total count you will have to build messages into your application protocol yourself ... which kinds of defeats the point of multicast.

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