QNetworkInterface 類

QNetworkInterface 類提供主機 IP 地址和網絡接口的列錶。 更多...

頭: #include <QNetworkInterface>
qmake: QT += network
Since: Qt 4.2

該類在 Qt 4.2 引入。

注意: 此類的所有函數 可重入 .

公共類型

enum InterfaceFlag { IsUp, IsRunning, CanBroadcast, IsLoopBack, IsPointToPoint, CanMulticast }
flags InterfaceFlags
enum InterfaceType { Unknown, Loopback, Virtual, Ethernet, Wifi, …, Ieee1394 }

公共函數

QNetworkInterface (const QNetworkInterface & other )
QNetworkInterface ()
QNetworkInterface & operator= (const QNetworkInterface & other )
~QNetworkInterface ()
QList<QNetworkAddressEntry> addressEntries () const
QNetworkInterface::InterfaceFlags flags () const
QString hardwareAddress () const
QString humanReadableName () const
int index () const
bool isValid () const
int maximumTransmissionUnit () const
QString name () const
void swap (QNetworkInterface & other )
QNetworkInterface::InterfaceType type () const

靜態公共成員

QList<QHostAddress> allAddresses ()
QList<QNetworkInterface> allInterfaces ()
QNetworkInterface interfaceFromIndex (int index )
QNetworkInterface interfaceFromName (const QString & name )
int interfaceIndexFromName (const QString & name )
QString interfaceNameFromIndex (int index )

詳細描述

QNetworkInterface represents one network interface attached to the host where the program is being run. Each network interface may contain zero or more IP addresses, each of which is optionally associated with a netmask and/or a broadcast address. The list of such trios can be obtained with addressEntries (). Alternatively, when the netmask or the broadcast addresses or other information aren't necessary, use the allAddresses () convenience function to obtain just the IP addresses of the active interfaces.

QNetworkInterface also reports the interface's hardware address with hardwareAddress ().

Not all operating systems support reporting all features. Only the IPv4 addresses are guaranteed to be listed by this class in all platforms. In particular, IPv6 address listing is only supported on Windows, Linux, macOS and the BSDs.

另請參閱 QNetworkAddressEntry .

成員類型文檔編製

enum QNetworkInterface:: InterfaceFlag
flags QNetworkInterface:: InterfaceFlags

指定關聯此網絡接口的標誌。可能的值:

常量 描述
QNetworkInterface::IsUp 0x1 the network interface is "up" - enabled by administrative action
QNetworkInterface::IsRunning 0x2 the network interface is operational: configured "up" and (typically) physically connected to a network
QNetworkInterface::CanBroadcast 0x4 the network interface works in broadcast mode
QNetworkInterface::IsLoopBack 0x8 the network interface is a loopback interface: that is, it's a virtual interface whose destination is the host computer itself
QNetworkInterface::IsPointToPoint 0x10 the network interface is a point-to-point interface: that is, there is one, single other address that can be directly reached by it.
QNetworkInterface::CanMulticast 0x20 the network interface supports multicasting

Note that one network interface cannot be both broadcast-based and point-to-point.

InterfaceFlags 類型是 typedef 對於 QFlags <InterfaceFlag>. It stores an OR combination of InterfaceFlag values.

enum QNetworkInterface:: InterfaceType

Specifies the type of hardware (PHY layer, OSI level 1) this interface is, if it could be determined. Interface types that are not among those listed below will generally be listed as Unknown, though future versions of Qt may add new enumeration values.

可能的值包括:

常量 描述
QNetworkInterface::Unknown 0 The interface type could not be determined or is not one of the other listed types.
QNetworkInterface::Loopback 1 The virtual loopback interface, which is assigned the loopback IP addresses (127.0.0.1, ::1).
QNetworkInterface::Virtual 2 A type of interface determined to be virtual, but not any of the other possible types. For example, tunnel interfaces are (currently) detected as virtual ones.
QNetworkInterface::Ethernet 3 IEEE 802.3 Ethernet interfaces, though on many systems other types of IEEE 802 interfaces may also be detected as Ethernet (especially Wi-Fi).
QNetworkInterface::Wifi 8 IEEE 802.11 Wi-Fi 接口。注意:在某些係統, QNetworkInterface 可能無法區分常規 Ethernet (以太網) 和 Wi-Fi,因此不會返迴此枚舉值。
QNetworkInterface::Ieee80211 Wifi WiFi 的彆名。
QNetworkInterface::CanBus 5 ISO 11898 Controller Area Network bus interfaces, usually found on automotive systems.
QNetworkInterface::Fddi 7 ANSI X3T12 Fiber Distributed Data Interface, a local area network over optical fibers.
QNetworkInterface::Ppp 6 Point-to-Point Protocol interfaces, establishing a direct connection between two nodes over a lower transport layer (often serial over radio or physical line).
QNetworkInterface::Slip 4 Serial Line Internet Protocol (串行綫路網際協議) 接口。
QNetworkInterface::Phonet 9 Interfaces using the Linux Phonet socket family, for communication with cellular modems. See the Linux kernel documentation 瞭解更多信息。
QNetworkInterface::Ieee802154 10 IEEE 802.15.4 Personal Area Network interfaces, other than 6LoWPAN (see below).
QNetworkInterface::SixLoWPAN 11 6LoWPAN (IPv6 over Low-power Wireless Personal Area Networks) interfaces, which operate on IEEE 802.15.4 PHY, but have specific header compression schemes for IPv6 and UDP. This type of interface is often used for mesh networking.
QNetworkInterface::Ieee80216 12 IEEE 802.16 Wireless Metropolitan Area Network, also known under the commercial name "WiMAX".
QNetworkInterface::Ieee1394 13 IEEE 1394 interfaces (a.k.a. "FireWire").

成員函數文檔編製

QNetworkInterface:: QNetworkInterface (const QNetworkInterface & other )

創建副本為 QNetworkInterface 對象包含在 other .

QNetworkInterface:: QNetworkInterface ()

構造空的網絡接口對象。

QNetworkInterface &QNetworkInterface:: operator= (const QNetworkInterface & other )

拷貝內容從 QNetworkInterface 對象包含在 other 到此。

QNetworkInterface:: ~QNetworkInterface ()

釋放資源關聯 QNetworkInterface 對象。

QList < QNetworkAddressEntry > QNetworkInterface:: addressEntries () const

Returns the list of IP addresses that this interface possesses along with their associated netmasks and broadcast addresses.

If the netmask or broadcast address or other information is not necessary, you can call the allAddresses () function to obtain just the IP addresses of the active interfaces.

[static] QList < QHostAddress > QNetworkInterface:: allAddresses ()

此方便函數返迴在主機中找到的所有 IP 地址。相當於調用 addressEntries () on all the objects returned by allInterfaces () that are in the QNetworkInterface::IsUp state to obtain lists of QNetworkAddressEntry objects then calling QNetworkAddressEntry::ip () on each of these.

[static] QList < QNetworkInterface > QNetworkInterface:: allInterfaces ()

Returns a listing of all the network interfaces found on the host machine. In case of failure it returns a list with zero elements.

QNetworkInterface::InterfaceFlags QNetworkInterface:: flags () const

返迴此網絡接口關聯的標誌。

QString QNetworkInterface:: hardwareAddress () const

Returns the low-level hardware address for this interface. On Ethernet interfaces, this will be a MAC address in string representation, separated by colons.

Other interface types may have other types of hardware addresses. Implementations should not depend on this function returning a valid MAC address.

另請參閱 type ().

QString QNetworkInterface:: humanReadableName () const

Returns the human-readable name of this network interface on Windows, such as "Local Area Connection", if the name could be determined. If it couldn't, this function returns the same as name (). The human-readable name is a name that the user can modify in the Windows Control Panel, so it may change during the execution of the program.

On Unix, this function currently always returns the same as name (), since Unix systems don't store a configuration for human-readable names.

該函數在 Qt 4.5 引入。

int QNetworkInterface:: index () const

Returns the interface system index, if known. This is an integer assigned by the operating system to identify this interface and it generally doesn't change. It matches the scope ID field in IPv6 addresses.

此函數返迴 0,若索引未知。

該函數在 Qt 4.5 引入。

[static] QNetworkInterface QNetworkInterface:: interfaceFromIndex ( int index )

返迴 QNetworkInterface object for the interface whose internal ID is index . Network interfaces have a unique identifier called the "interface index" to distinguish it from other interfaces on the system. Often, this value is assigned progressively and interfaces being removed and then added again get a different value every time.

This index is also found in the IPv6 address' scope ID field.

[static] QNetworkInterface QNetworkInterface:: interfaceFromName (const QString & name )

返迴 QNetworkInterface object for the interface named name . If no such interface exists, this function returns an invalid QNetworkInterface 對象。

字符串 name may be either an actual interface name (such as "eth0" or "en1") or an interface index in string form ("1", "2", etc.).

另請參閱 name () 和 isValid ().

[static] int QNetworkInterface:: interfaceIndexFromName (const QString & name )

Returns the index of the interface whose name is name or 0 if there is no interface with that name. This function should produce the same result as the following code, but will probably execute faster.

    QNetworkInterface::interfaceFromName(name).index()
					

該函數在 Qt 5.7 引入。

另請參閱 interfaceFromName (), interfaceNameFromIndex (),和 QNetworkDatagram::interfaceIndex ().

[static] QString QNetworkInterface:: interfaceNameFromIndex ( int index )

Returns the name of the interface whose index is index or an empty string if there is no interface with that index. This function should produce the same result as the following code, but will probably execute faster.

    QNetworkInterface::interfaceFromIndex(index).name()
					

該函數在 Qt 5.7 引入。

另請參閱 interfaceFromIndex (), interfaceIndexFromName (),和 QNetworkDatagram::interfaceIndex ().

bool QNetworkInterface:: isValid () const

返迴 true 若此 QNetworkInterface object contains valid information about a network interface.

int QNetworkInterface:: maximumTransmissionUnit () const

Returns the maximum transmission unit on this interface, if known, or 0 otherwise.

The maximum transmission unit is the largest packet that may be sent on this interface without incurring link-level fragmentation. Applications may use this value to calculate the size of the payload that will fit an unfragmented UDP datagram. Remember to subtract the sizes of headers used in your communication over the interface, e.g. TCP (20 bytes) or UDP (12), IPv4 (20) or IPv6 (40, absent some form of header compression), when computing how big a payload you can transmit. Also note that the MTU along the full path (the Path MTU) to the destination may be smaller than the interface's MTU.

該函數在 Qt 5.11 引入。

另請參閱 QUdpSocket .

QString QNetworkInterface:: name () const

Returns the name of this network interface. On Unix systems, this is a string containing the type of the interface and optionally a sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's an internal ID that cannot be changed by the user.

void QNetworkInterface:: swap ( QNetworkInterface & other )

Swaps this network interface instance with other 。此函數非常快且從不失敗。

該函數在 Qt 5.0 引入。

QNetworkInterface::InterfaceType QNetworkInterface:: type () const

Returns the type of this interface, if it could be determined. If it could not be determined, this function returns QNetworkInterface::Unknown .

該函數在 Qt 5.11 引入。

另請參閱 hardwareAddress ().