开发者

UDP listening socket stops listening after network configuration changes

开发者 https://www.devze.com 2023-04-12 15:11 出处:网络
I have a UDP socket listening in a port for broadcast transmissions and it is working fine. However, when I hibernate and resume the OS (Windows 7), the socket just stops receving data (and I can see

I have a UDP socket listening in a port for broadcast transmissions and it is working fine.

However, when I hibernate and resume the OS (Windows 7), the socket just stops receving data (and I can see that there is data arriving using Wireshark).

This also happens if I change any network setting开发者_运维技巧s like, change my IP address, disable and enable the network adapter.

The OS seems to disable all network adapters when hibernating and to re-enable them when it is resumed.

select just returns 0 (timeout) which is no different than when I'm not receiving any data. I could not find any references to this behavior anywhere.

If I close the socket and recreate it, it starts to work again.

My TCP listening sockets still working fine after resuming the OS.

Any ideas on how detect and correct this situation?

EDIT: It still receiving directly addresses data just fine, it just does not receive brodcast transmissions anymore.

EDIT2: Just discovered that if I write to the socket (send a dummy packet to anywhere) it starts to work again...


I think your code does not explicitly binds the socket to "0.0.0.0" address. So when you do sentto it binds the interface IP which is available at that time. When this IP is changed or interface is disabled, this socket will be reset by the TCP/IP stack. In your TCP socket, you should have bound to "0.0.0.0" address so it will always listen for connection independent of any IP/interface changes. You can make your udp socket also bound to "0.0.0.0" before sending any data on it. This will make it work even after hibernation or IP changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号