定期检测IP地址更改 [英] Periodically detecting IP address change

查看:152
本文介绍了定期检测IP地址更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在编写程序,需要确保某个变量在这种情况下为IP地址具有正确的数字.

因此,我正在考虑创建一个线程来检查IP地址是否每5分钟更改一次,但我尚未编写代码,但是看起来像这样

Hello,

I''m writing a program and i need to make sure that a certain variable has the correct number in this case an IP address.

So i''m thinking of creating a thread that checks if the IP address changes every 5 minutes i haven''t written the code but it will look something like this

while(true)
{
   Sleep(300000); //5 minutes
   GetIPAddres();
   if different updates working thread
}



因此,当变量更改其内容时,如何在这种情况下更新主线程.



So how can i update my working thread in this case will be main when the variable changes it''s content

推荐答案

由于您的PC可以有多个IP地址,因此一个对于每个活动适配器,您可能只需要一个调用就需要进行更复杂的检查.

另外,您不必轮询或使用计时器,只需启动一个等待
Since your PC can have multiple IP addresses, one for each active adapter, you may need more complicated checks that just that one call.

Also, you don''t have to poll or use timers, just start a thread that wait for
NotifyAddrChange(NULL, NULL)

的线程,然后在网络地址更改时采取任何必要的操作即可.

网络反弹时会触发此事件,因此地址可能与上次相同.但是至少您知道什么时候会发生一些有趣的事情,而不是每隔5分钟轮询一次.

and then take whatever action is necessary when the network address changes.

This event is triggered when the network bounces so the address may be that same as the last time but at least you know when something interesting happens rather than polling every 5 minutes.


如果您"为Windows操作系统重新编码,则可以为此目的使用计时器(不创建新线程),请参见 [
If you''re coding for the Windows OS, then you may use a timer (without creating a new thread) for the purpose, see "Using Timers" at MSDN[^].


这篇关于定期检测IP地址更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆