[英]Detect that a server is running locally by its IP address
My program uses Bonjour to get a list of servers running on various IP addresses on the local network, but one or more of them might be running on the same machine. 我的程序使用Bonjour获取在本地网络上的各种IP地址上运行的服务器列表,但是其中一个或多个服务器可能在同一台计算机上运行。
I need to know if a server is running on the same machine, by checking its IP address. 我需要通过检查其IP地址来了解服务器是否在同一台计算机上运行。
For example, servers on 127.0.0.1
, 192.168.0.100
, and 192.168.56.1
are all running on my local machine, but a server on 192.168.0.104
or on 192.168.56.2
would be running on another machine. 例如,在服务器
127.0.0.1
, 192.168.0.100
和192.168.56.1
都是我的本地机器上运行,但在服务器192.168.0.104
或192.168.56.2
将另一台机器上运行。
The QNeworkInterfaces class has a static function that you can call: - QNeworkInterfaces类有一个可以调用的静态函数: -
QList<QHostAddress> addressList = QNetworkInterfaces::allAddresses();
You can then iterate through the addressList and compare those with the server addresses: - 然后,您可以遍历addressList并将其与服务器地址进行比较: -
bool IsLocalServer()
{
QList<QHostAddress> addressList = QNetworkInterfaces::allAddresses();
foreach(QHostAddress address, addressList)
{
if(address == QHostAddress("192.168.0.100")
return true;
else if(address == QHostAddress("192.168.56.1")
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.