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?
-
I would separate the address resolution from the
UdpClient
.- Use
Dns
class to resolveIPaddress
(and store in local variable) - Use the parameterless constructor of
UdpClient
, - Don't do a connect on
UdpClient
- Use the explicit
Send
with theIPEndPoint
parameter.
On a background thread:
- check every X seconds for updated DNS
- replace the local
IPEndPoint
variable you pass to yourUdpClient.Send
call.
No need to destroy your
UdpClient
every time you do this. Also, no need to lock when updating theIPEndPoint
. 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. - Use
0 comments:
Post a Comment