views:

160

answers:

1

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:

  1. 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.
  2. 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?

+2  A: 

I would separate the address resolution from the UdpClient.

  1. Use Dns class to resolve IPaddress (and store in local variable)
  2. Use the parameterless constructor of UdpClient,
  3. Don't do a connect on UdpClient
  4. Use the explicit Send with the IPEndPoint parameter.

On a background thread:

  1. check every X seconds for updated DNS
  2. replace the local IPEndPoint variable you pass to your UdpClient.Send call.

No need to destroy your UdpClient every time you do this. Also, no need to lock when updating the IPEndPoint. The worse case is that you have one dirty send to an old address, but since you are not instantly notified on updates, you will have dirty sends anyway.

Erich Mirabal