如何确定重新配置的网络适配器是否准备好传输?

发布于 2025-01-03 11:37:44 字数 587 浏览 1 评论 0原文

我在以下过程中遇到问题:

  1. 我的应用程序调用以管理员身份运行的 Windows 服务来根据需要设置网络适配器的 IP 配置。
  2. 由于 Windows 服务无法轻松发出其已完成任务的信号,因此应用程序会监视网络适配器,直到出现所需的配置(有超时)。
  3. 应用程序通过 TCP 连接到目标 IP 地址。

我的问题是配置网络适配器(步骤 2)和可以使用网络适配器(步骤 3)之间存在延迟。如果我在重新配置适配器后尝试立即 ping 或通过 TCP 连接,则会收到错误:

无法到达目标主机。

如果我添加任意 5 秒的延迟,它可以正常工作,但这是一个糟糕的解决方案。我正在使用 Win32_NetworkAdapterConfiguration< /a> 用于重新配置和监视网络适配器的 WMI 类。我该怎么做才能确保适配器准备好传输而不会有竞争条件的风险?

I have a problem with the following process:

  1. My application calls a Windows Service running as Administrator to set the network adapter's IP configuration as required.
  2. Since a Windows Service cannot easily signal that it has completed the task, the application watches the network adapter until the required configuration is present (with a timeout).
  3. The application connects to the target IP address by TCP.

My problem is there is a delay between when the network adapter is configured (step 2) and when it can be used (step 3.) If I try to ping or connect via TCP immediately after reconfiguring the adapter, I get the error:

Destination host cannot be reached.

If I include an arbitrary 5-second delay, it works fine, but this is a terrible solution. I'm using the Win32_NetworkAdapterConfiguration WMI class to reconfigure and watch the network adapter. What can I do to ensure the adapter is ready to transmit without risking a race condition?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文