QHostAddress 類

The QHostAddress 類提供 IP 地址。 更多...

頭: #include <QHostAddress>

公共類型

enum SpecialAddress { Null, LocalHost, LocalHostIPv6, Broadcast, Any, AnyIPv6 }

公共函數

QHostAddress ()
QHostAddress (quint32 ip4Addr )
QHostAddress (quint8 * ip6Addr )
QHostAddress (const Q_IPV6ADDR & ip6Addr )
QHostAddress (const sockaddr * sockaddr )
QHostAddress (const QString & address )
QHostAddress (const QHostAddress & address )
QHostAddress (SpecialAddress address )
~QHostAddress ()
void clear ()
bool isInSubnet (const QHostAddress & subnet , int netmask ) const
bool isInSubnet (const QPair<QHostAddress, int> & subnet ) const
bool isNull () const
QAbstractSocket::NetworkLayerProtocol protocol () const
QString scopeId () const
void setAddress (quint32 ip4Addr )
void setAddress (quint8 * ip6Addr )
void setAddress (const Q_IPV6ADDR & ip6Addr )
bool setAddress (const QString & address )
void setAddress (const sockaddr * sockaddr )
void setScopeId (const QString & id )
quint32 toIPv4Address () const
Q_IPV6ADDR toIPv6Address () const
QString toString () const
bool operator!= (const QHostAddress & other ) const
bool operator!= (SpecialAddress other ) const
QHostAddress & operator= (const QHostAddress & address )
QHostAddress & operator= (const QString & address )
bool operator== (const QHostAddress & other ) const
bool operator== (SpecialAddress other ) const

靜態公共成員

QPair<QHostAddress, int> parseSubnet (const QString & subnet )
QDataStream & operator<< (QDataStream & out , const QHostAddress & address )
QDataStream & operator>> (QDataStream & in , QHostAddress & address )

詳細描述

The QHostAddress 類提供 IP 地址。

此類以與平颱和協議無關的方式保持 IPv4 或 IPv6 地址。

QHostAddress 通常用於 QTcpSocket , QTcpServer ,和 QUdpSocket 以連接到主機或設置服務器。

設置主機地址采用 setAddress (),和檢索采有 toIPv4Address (), toIPv6Address (),或 toString ()。可以檢查類型采用 protocol ().

注意: 請注意 QHostAddress 不做 DNS 查找。 QHostInfo 是需要的為此。

類還支持常見預定義地址: Null , LocalHost , LocalHostIPv6 , Broadcast ,和 Any .

另請參閱 QHostInfo , QTcpSocket , QTcpServer ,和 QUdpSocket .

成員類型文檔編製

enum QHostAddress:: SpecialAddress

常量 描述
QHostAddress::Null 0 null 地址對象。相當於 QHostAddress ().
QHostAddress::LocalHost 2 Pv4 本地主機地址。相當於 QHostAddress ("127.0.0.1")。
QHostAddress::LocalHostIPv6 3 IPv6 本地主機地址。相當於 QHostAddress ("::1").
QHostAddress::Broadcast 1 IPv4 廣播地址。相當於 QHostAddress ("255.255.255.255")。
QHostAddress::Any 4 IPv4 任意地址。相當於 QHostAddress ("0.0.0.0").
QHostAddress::AnyIPv6 5 IPv6 任意地址。相當於 QHostAddress ("::").

成員函數文檔編製

QHostAddress:: QHostAddress ()

Constructs a host address object with the IP address 0.0.0.0.

另請參閱 clear ().

QHostAddress:: QHostAddress ( quint32 ip4Addr )

構造主機地址對象采用 IPv4 地址 ip4Addr .

QHostAddress:: QHostAddress ( quint8 * ip6Addr )

構造主機地址對象采用 IPv6 地址 ip6Addr .

ip6Addr 必須是網絡字節序 (大端在前) 的 16 字節數組。

QHostAddress:: QHostAddress (const Q_IPV6ADDR & ip6Addr )

構造主機地址對象采用 IPv6 地址 ip6Addr .

QHostAddress:: QHostAddress (const sockaddr * sockaddr )

構造 IPv4 或 IPv6 地址使用指定地址通過本機結構 sockaddr .

另請參閱 setAddress ().

QHostAddress:: QHostAddress (const QString & address )

構造 IPv4 或 IPv6 地址基於字符串 address (如 127.0.0.1)。

另請參閱 setAddress ().

QHostAddress:: QHostAddress (const QHostAddress & address )

構造副本為給定 address .

QHostAddress:: QHostAddress ( SpecialAddress address )

構造 QHostAddress 對象為 address .

QHostAddress:: ~QHostAddress ()

銷毀主機地址對象。

void QHostAddress:: clear ()

Sets the host address to 0.0.0.0.

bool QHostAddress:: isInSubnet (const QHostAddress & subnet , int netmask ) const

Returns true if this IP is in the subnet described by the network prefix subnet and netmask netmask .

An IP is considered to belong to a subnet if it is contained between the lowest and the highest address in that subnet. In the case of IP version 4, the lowest address is the network address, while the highest address is the broadcast address.

The subnet argument does not have to be the actual network address (the lowest address in the subnet). It can be any valid IP belonging to that subnet. In particular, if it is equal to the IP address held by this object, this function will always return true (provided the netmask is a valid value).

該函數在 Qt 4.5 引入。

另請參閱 parseSubnet ().

bool QHostAddress:: isInSubnet (const QPair < QHostAddress , int > & subnet ) const

這是重載函數。

Returns true if this IP is in the subnet described by subnet QHostAddress member of subnet contains the network prefix and the int (second) member contains the netmask (prefix length).

該函數在 Qt 4.5 引入。

bool QHostAddress:: isNull () const

Returns true if this host address is null (INADDR_ANY or in6addr_any). The default constructor creates a null address, and that address is not valid for any host or interface.

[static] QPair < QHostAddress , int > QHostAddress:: parseSubnet (const QString & subnet )

Parses the IP and subnet information contained in subnet and returns the network prefix for that network and its prefix length.

The IP address and the netmask must be separated by a slash (/).

此函數支持的自變量形式:

  • 123.123.123.123/n where n is any value between 0 and 32
  • 123.123.123.123/255.255.255.255
  • <ipv6-address>/n where n is any value between 0 and 128

For IP version 4, this function accepts as well missing trailing components (i.e., less than 4 octets, like "192.168.1"), followed or not by a dot. If the netmask is also missing in that case, it is set to the number of octets actually passed (in the example above, it would be 24, for 3 octets).

該函數在 Qt 4.5 引入。

另請參閱 isInSubnet ().

QAbstractSocket::NetworkLayerProtocol QHostAddress:: protocol () const

返迴主機地址的網絡層協議。

QString QHostAddress:: scopeId () const

返迴 IPv6 地址的作用域 ID。對於 IPv4 地址,或者若地址不包含作用域 ID,空 QString 被返迴。

The IPv6 scope ID specifies the scope of reachability for non-global IPv6 addresses, limiting the area in which the address can be used. All IPv6 addresses are associated with such a reachability scope. The scope ID is used to disambiguate addresses that are not guaranteed to be globally unique.

IPv6 specifies the following four levels of reachability:

  • Node-local: Addresses that are only used for communicating with services on the same interface (e.g., the loopback interface "::1").
  • Link-local: Addresses that are local to the network interface ( link ). There is always one link-local address for each IPv6 interface on your host. Link-local addresses ("fe80...") are generated from the MAC address of the local network adaptor, and are not guaranteed to be unique.
  • Site-local: Addresses that are local to the site / private network (e.g., the company intranet). Site-local addresses ("fec0...") are usually distributed by the site router, and are not guaranteed to be unique outside of the local site.
  • Global: For globally routable addresses, such as public servers on the Internet.

When using a link-local or site-local address for IPv6 connections, you must specify the scope ID. The scope ID for a link-local address is usually the same as the interface name (e.g., "eth0", "en1") or number (e.g., "1", "2").

該函數在 Qt 4.1 引入。

另請參閱 setScopeId ().

void QHostAddress:: setAddress ( quint32 ip4Addr )

設置 IPv4 地址指定通過 ip4Addr .

void QHostAddress:: setAddress ( quint8 * ip6Addr )

這是重載函數。

設置 IPv6 地址指定通過 ip6Addr .

ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).

void QHostAddress:: setAddress (const Q_IPV6ADDR & ip6Addr )

這是重載函數。

設置 IPv6 地址指定通過 ip6Addr .

bool QHostAddress:: setAddress (const QString & address )

這是重載函數。

Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.g. "127.0.0.1"). Returns true and sets the address if the address was successfully parsed; otherwise returns false.

void QHostAddress:: setAddress (const sockaddr * sockaddr )

這是重載函數。

Sets the IPv4 or IPv6 address specified by the native structure sockaddr . Returns true and sets the address if the address was successfully parsed; otherwise returns false.

void QHostAddress:: setScopeId (const QString & id )

Sets the IPv6 scope ID of the address to id . If the address protocol is not IPv6, this function does nothing.

該函數在 Qt 4.1 引入。

另請參閱 scopeId ().

quint32 QHostAddress:: toIPv4Address () const

以數字形式返迴 IPv4 地址。

例如:若地址為 127.0.0.1,返迴值為 2130706433 (即 0x7f000001)。

This value is only valid if the Protocol() is IPv4Protocol .

另請參閱 toString ().

Q_IPV6ADDR QHostAddress:: toIPv6Address () const

以 Q_IPV6ADDR 結構形式返迴 IPv6 地址。結構由 16 個無符號字符組成。

Q_IPV6ADDR addr = hostAddr.toIPv6Address();
// addr contains 16 unsigned characters
for (int i = 0; i < 16; ++i) {
    // process addr[i]
}
					

This value is only valid if the protocol () 是 IPv6Protocol .

另請參閱 toString ().

QString QHostAddress:: toString () const

以字符串形式返迴地址。

For example, if the address is the IPv4 address 127.0.0.1, the returned string is "127.0.0.1". For IPv6 the string format will follow the RFC5952 recommendation.

另請參閱 toIPv4Address ().

bool QHostAddress:: operator!= (const QHostAddress & other ) const

Returns true if this host address is not the same as the other address given; otherwise returns false.

該函數在 Qt 4.2 引入。

bool QHostAddress:: operator!= ( SpecialAddress other ) const

Returns true if this host address is not the same as the other address given; otherwise returns false.

QHostAddress & QHostAddress:: operator= (const QHostAddress & address )

賦值另一主機 address 到此對象,並返迴此對象的引用。

QHostAddress & QHostAddress:: operator= (const QString & address )

賦值主機地址 address 到此對象,並返迴此對象的引用。

另請參閱 setAddress ().

bool QHostAddress:: operator== (const QHostAddress & other ) const

Returns true if this host address is the same as the other address given; otherwise returns false.

bool QHostAddress:: operator== ( SpecialAddress other ) const

Returns true if this host address is the same as the other address given; otherwise returns false.

相關非成員

QDataStream & operator<< ( QDataStream & out , const QHostAddress & address )

寫入主機地址 address 到流 out 並返迴流引用。

另請參閱 序列化 Qt 數據類型 .

QDataStream & operator>> ( QDataStream & in , QHostAddress & address )

把主機地址讀入 address 從流 in 並返迴流引用。

另請參閱 序列化 Qt 數據類型 .