Is there a cross-platform way to get the local IP address (i.e. something that looks like 192.168.1.49
) of the computer using Qt?
是否有跨平台的方式使用Qt获取计算机的本地IP地址(例如看起来像192.168.1.49)?
I want to make an FTP server for a Symbian phone and I want to show the IP address the FTP client should connect to.
我想为Symbian手机制作一个FTP服务器,我想显示FTP客户端应该连接的IP地址。
5 个解决方案
#1
36
Use QNetworkInterface::allAddresses()
使用QNetworkInterface::allAddresses()
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
qDebug() << address.toString();
}
#2
13
QNetworkInterface::allAddresses()
will give you the network addresses. You can then filter the results to IPv4 addresses that are not loopback addresses:
alladdress()将为您提供网络地址。然后,您可以将结果过滤到非回送地址的IPv4地址:
QList<QHostAddress> list = QNetworkInterface::allAddresses();
for(int nIter=0; nIter<list.count(); nIter++)
{
if(!list[nIter].isLoopback())
if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
qDebug() << list[nIter].toString();
}
#3
5
If you require more information than just IP addresses (like the subnet), you have to iterate over all the interfaces.
如果您需要的信息不仅仅是IP地址(比如子网),那么您必须对所有接口进行迭代。
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QNetworkInterface eth;
foreach(eth, allInterfaces) {
QList<QNetworkAddressEntry> allEntries = eth.addressEntries();
QNetworkAddressEntry entry;
foreach (entry, allEntries) {
qDebug() << entry.ip().toString() << "/" << entry.netmask().toString();
}
}
#4
2
QNetworkInterface returns lots of addresses. you must filter them, to get desirable result:
QNetworkInterface返回很多地址。你必须过滤它们,以得到理想的结果:
foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
QNetworkInterface::InterfaceFlags flags = netInterface.flags();
if( (bool)(flags & QNetworkInterface::IsRunning) && !(bool)(flags & QNetworkInterface::IsLoopBack)){
foreach (const QNetworkAddressEntry &address, netInterface.addressEntries()) {
if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
qDebug() << address.ip().toString();
}
}
}
#5
1
I wanted to get eth1
IP address of my target machine. Answers provided above helped me to get what I wanted: This is how I wrote my function to get me the IP address of the network interface name eth1
.
我想获取目标机器的eth1 IP地址。上面提供的答案帮助我得到了我想要的:这就是我如何编写函数来获得网络接口名称eth1的IP地址。
QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName("eth1");
QList<QNetworkAddressEntry> entries = eth1Ip.addressEntries();
if (!entries.isEmpty()) {
QNetworkAddressEntry entry = entries.first();
qDebug() << entry.ip();
}
#1
36
Use QNetworkInterface::allAddresses()
使用QNetworkInterface::allAddresses()
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
qDebug() << address.toString();
}
#2
13
QNetworkInterface::allAddresses()
will give you the network addresses. You can then filter the results to IPv4 addresses that are not loopback addresses:
alladdress()将为您提供网络地址。然后,您可以将结果过滤到非回送地址的IPv4地址:
QList<QHostAddress> list = QNetworkInterface::allAddresses();
for(int nIter=0; nIter<list.count(); nIter++)
{
if(!list[nIter].isLoopback())
if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
qDebug() << list[nIter].toString();
}
#3
5
If you require more information than just IP addresses (like the subnet), you have to iterate over all the interfaces.
如果您需要的信息不仅仅是IP地址(比如子网),那么您必须对所有接口进行迭代。
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QNetworkInterface eth;
foreach(eth, allInterfaces) {
QList<QNetworkAddressEntry> allEntries = eth.addressEntries();
QNetworkAddressEntry entry;
foreach (entry, allEntries) {
qDebug() << entry.ip().toString() << "/" << entry.netmask().toString();
}
}
#4
2
QNetworkInterface returns lots of addresses. you must filter them, to get desirable result:
QNetworkInterface返回很多地址。你必须过滤它们,以得到理想的结果:
foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
QNetworkInterface::InterfaceFlags flags = netInterface.flags();
if( (bool)(flags & QNetworkInterface::IsRunning) && !(bool)(flags & QNetworkInterface::IsLoopBack)){
foreach (const QNetworkAddressEntry &address, netInterface.addressEntries()) {
if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
qDebug() << address.ip().toString();
}
}
}
#5
1
I wanted to get eth1
IP address of my target machine. Answers provided above helped me to get what I wanted: This is how I wrote my function to get me the IP address of the network interface name eth1
.
我想获取目标机器的eth1 IP地址。上面提供的答案帮助我得到了我想要的:这就是我如何编写函数来获得网络接口名称eth1的IP地址。
QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName("eth1");
QList<QNetworkAddressEntry> entries = eth1Ip.addressEntries();
if (!entries.isEmpty()) {
QNetworkAddressEntry entry = entries.first();
qDebug() << entry.ip();
}