如何让 UdpClient 对 DNS 更新做出反应
我正在使用 UdpClient 将数据包发送到服务器。 我在构造 Sender 对象时使用(主机名,端口)构造函数初始化 UdpClient。 当以这种方式构造时,UdpClient 将主机名解析为 IP 地址。 对 UdpClient 对象的后续调用将使用该 IP 地址。
不幸的是,如果使用的 DNS 别名通过 DNS 系统更新为指向不同的 IP 地址,则此更改不会反映在我的 Sender 对象中,除非重新创建它。
让我的发件人对象及时响应 DNS 更改的最佳方式是什么? 性能非常重要。 我可以想到几种解决方案:
- 在每次调用时进行 DNS 解析(即使用接受主机名参数的发送重载)。 我不知道,由于 Windows DNS 缓存的原因,这实际上可能相当快。
- 在计时器线程上运行某种 DNS 检查器,定期检查 DNS 别名是否解析为不同的 IP。 如果是这样,它会以某种方式更新 UdpClient 以使用新的 IP 地址。 但是,我真的不想在每次调用时锁定 UdpClient 对象 - 正如我所说,性能很重要。
有人有这样做的经验吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将地址解析与 UdpClient 分开。
Dns
类到解析IP地址
(并存储在局部变量)UdpClient
的无参数构造函数,UdpClient
上进行连接IPEndPoint< 的显式
Send
/代码> 参数。在后台线程上:
UdpClient.Send
调用的本地IPEndPoint
变量。无需每次执行此操作时都销毁您的
UdpClient
。此外,更新
IPEndPoint
时无需锁定。 最糟糕的情况是,您有一个脏发送发送到旧地址,但由于您没有立即收到更新通知,因此无论如何您都会收到脏发送。I would separate the address resolution from the
UdpClient
.Dns
class to resolveIPaddress
(and store in local variable)UdpClient
,UdpClient
Send
with theIPEndPoint
parameter.On a background thread:
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 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.