Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP listening socket stops listening after network configuration changes

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 settings 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...

like image 883
Vargas Avatar asked Oct 13 '11 14:10

Vargas


1 Answers

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.

like image 120
dvasanth Avatar answered Sep 18 '22 12:09

dvasanth