I am using a UdpClient to send packets to a server. I am initializing the UdpClient upon construction of my Sender object, using the (hostname, port) constructor. When constructed in this manner, the UdpClient resolves the hostname to an IP address. Subsequent calls to the UdpClient object use the IP address.
Unfortunately, if the DNS alias used is updated through the DNS system to point to a different IP address, this change is not reflected in my Sender object unless it is re-created.
What would be the best way to have my Sender object react to DNS changes in a timely manner? Performance is very important. I can think of several solutions:
- Doing a DNS resolve on every call (i.e using the Send overload which accepts a hostname parameter). This may actually be quite fast because of the Windows DNS cache, I don't know.
- Having some sort of DNS checker running on a timer thread to check periodically if the DNS alias resolves to a different IP. If it does, it would somehow update the UdpClient to use the new IP Address. However, I don't really want to be locking the UdpClient object on every call - as I said, performance is important.
Anybody got any experience of doing this?