如何让 UdpClient 对 DNS 更新做出反应

发布于 2024-07-17 10:11:56 字数 556 浏览 3 评论 0原文

我正在使用 UdpClient 将数据包发送到服务器。 我在构造 Sender 对象时使用(主机名,端口)构造函数初始化 UdpClient。 当以这种方式构造时,UdpClient 将主机名解析为 IP 地址。 对 UdpClient 对象的后续调用将使用该 IP 地址。

不幸的是,如果使用的 DNS 别名通过 DNS 系统更新为指向不同的 IP 地址,则此更改不会反映在我的 Sender 对象中,除非重新创建它。

让我的发件人对象及时响应 DNS 更改的最佳方式是什么? 性能非常重要。 我可以想到几种解决方案:

  1. 在每次调用时进行 DNS 解析(即使用接受主机名参数的发送重载)。 我不知道,由于 Windows DNS 缓存的原因,这实际上可能相当快。
  2. 在计时器线程上运行某种 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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-07-24 10:11:56

我会将地址解析与 UdpClient 分开。

  1. 使用 Dns 类到解析 IP地址(并存储在局部变量)
  2. 使用 UdpClient 的无参数构造函数,
  3. 不要在 UdpClient 上进行连接
  4. 使用带有 IPEndPoint< 的显式 Send /代码> 参数。

在后台线程上:

  1. 每 X 秒检查一次更新的 DNS,
  2. 替换您传递给 UdpClient.Send 调用的本地 IPEndPoint 变量。

无需每次执行此操作时都销毁您的 UdpClient
此外,更新 IPEndPoint 时无需锁定。 最糟糕的情况是,您有一个脏发送发送到旧地址,但由于您没有立即收到更新通知,因此无论如何您都会收到脏发送。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文