UDP listener stops listening after changing network configuration

I have a UDP socket listening on a broadcast port and it works fine.

However, when I winter and resume the OS (Windows 7), the socket simply stops receiving data (and I see that there is data coming in using Wireshark).

This also happens if I change any network settings, for example, change my IP address by disconnecting and activating the network adapter.

The OS seems to disable all network adapters during sleep mode and reactivate them when resuming.

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

If I close the socket and recreate it, it will start working again.

My cellular TCP listeners are still working fine after resuming the OS.

Any ideas on how to detect and fix this situation?

EDIT : it still receives direct address data only in order, it no longer receives brodcast transmissions.

EDIT2: Just found that if I write to a socket (send a dummy packet anywhere), it will start working again ...

+6
source share
1 answer

I think your code does not explicitly associate the socket with the address "0.0.0.0". Therefore, when you send a message, it binds the IP interface available at that time. When this IP address is changed or the interface is disconnected, this socket will be reset by the TCP / IP stack. On your TCP socket, you must bind to the address "0.0.0.0" so that it always listens for the connection, regardless of any IP / interface changes. You can make your udp socket also tied to "0.0.0.0" before sending any data to it. This will make it work even after hibernating or changing IP.

+1
source

Source: https://habr.com/ru/post/899228/


All Articles