3

I have a UDP server which binds to a well-known port, adds itself to a multicast group and listens for requests for clients. (The server is on Windows, and uses WSARecvFrom to issue an overlapped receive for client datagrams.) Clients send messages to the server at its well-known port and multicast IP address.

When testing the 'server' on a laptop, I have noticed that if the laptop enters a 'sleep' state the server's receiving socket becomes 'unbound' from the multicast address (so that client sends to the multicast address are no-longer received). However, the socket is still receiving in that a send to its port at 127.0.0.1 is still received - and Windows does not indicate any error. (The server socket is bound to INADDR_ANY.)

The 'server' is part of a peer-to-peer application, used for auto-discovery - so this situation is not as unusual as might be expected.

Can you suggest a way to determine whether the server is still actively listening on the multicast address without sending to the multicast address (which would result in unnecessary traffic to all 'servers' on the network)? One possible solution would be to send to the IP address of the adapter used for multicast, but I'm not sure how to determine this.

4

1 回答 1

7

您的网络堆栈将定期发送IGMP消息。您网络上的交换机可以使用IGMP 侦听来确定您的计算机需要哪些(如果有)多播消息。(这一切都是为了避免由于没有请求的流量而给主机施加过多的负载。)

当您的笔记本电脑休眠时,它将停止发送定期消息,以确认其对某些多播流量感兴趣。您的交换机会注意到这一点并停止将流量发送到您的笔记本电脑。

我认为您需要发现笔记本电脑何时从休眠状态恢复并将成员资格重新添加到多播组。我过去通过使用控制代码SIO_ADDRESS_LIST_CHANGE调用WSAIoctl来完成此操作(加上处理启用/禁用的网络适配器) 。每当笔记本电脑休眠时,所有适配器都会报告为已禁用;当它恢复时,将报告适配器再次启用。(请注意,这可能有点繁琐,因为适配器可能会在它们真正能够发送 UDP 数据报之前几秒钟报告为可用,因此您可能必须重试几次添加成员资格,每次重试之间会有小的延迟。或者您如果您愿意,可以在收到适配器更改通知后等待 30 秒。)

于 2012-10-18T09:37:57.890 回答