简介
用于表示网络接口(即网卡)信息
常用接口
static QList<QNetworkInterface> allInterfaces();
static QList<QHostAddress> allAddresses();
QList<QNetworkAddressEntry> addressEntries() const;
接口类型
用枚举InterfaceType
表示
enum InterfaceType {Loopback = 1,Virtual,Ethernet,Slip,CanBus,Ppp,Fddi,Wifi,Ieee80211 = Wifi, // aliasPhonet,Ieee802154,SixLoWPAN, // 6LoWPAN, but we can't start with a digitIeee80216,Ieee1394,Unknown = 0};
接口状态
用枚举InterfaceFlag
表示
enum InterfaceFlag {IsUp = 0x1,IsRunning = 0x2,CanBroadcast = 0x4,IsLoopBack = 0x8,IsPointToPoint = 0x10,CanMulticast = 0x20};
结构
QNetworkAddressEntry
:用来表示网络地址实体,即包含地址,广播地址以及掩码
QHostAddress
:包含地址以及协议
QHostAddressPrivate
成员包含:
QString scopeId;union {Q_IPV6ADDR a6; // IPv6 addressstruct { quint64 c[2]; } a6_64;struct { quint32 c[4]; } a6_32;
};
quint32 a; // IPv4 address
qint8 protocol;
QNetworkInterfaceManager
网络接口管理器,获取电脑上所有可用的网络接口
定义有全局静态变量
Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager)
其结构为
allInterfaces
:获取所有的网络接口,先通过scan
得到所有网络接口,winrt,win,unix不同平整对scan
有不同实现,然后调用 postProcess
作后处理
QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
{const QList<QNetworkInterfacePrivate *> list = postProcess(scan());QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;result.reserve(list.size());for (QNetworkInterfacePrivate *ptr : list) {if ((ptr->flags & QNetworkInterface::IsUp) == 0) {// if the network interface isn't UP, the addresses are ineligible for DNSfor (auto &addr : ptr->addressEntries)addr.setDnsEligibility(QNetworkAddressEntry::DnsIneligible);}result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);}return result;
}static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list)
{// Some platforms report a netmask but don't report a broadcast address// Go through all available addresses and calculate the broadcast address// from the IP and the netmask//// This is an IPv4-only thing -- IPv6 has no concept of broadcasts// The math is:// broadcast = IP | ~netmaskfor (QNetworkInterfacePrivate *interface : list) {for (QNetworkAddressEntry &address : interface->addressEntries) {if (address.ip().protocol() != QAbstractSocket::IPv4Protocol)continue;if (!address.netmask().isNull() && address.broadcast().isNull()) {QHostAddress bcast = address.ip();bcast = QHostAddress(bcast.toIPv4Address() | ~address.netmask().toIPv4Address());address.setBroadcast(bcast);}}}return list;
}