The QHostAddress 類提供 IP 地址。 更多...
| 頭: | #include <QHostAddress> |
| qmake: | QT += network |
| flags | ConversionMode |
| enum | ConversionModeFlag { StrictConversion, ConvertV4MappedToIPv4, ConvertV4CompatToIPv4, ConvertLocalHost, ConvertUnspecifiedAddress, TolerantConversion } |
| enum | SpecialAddress { Null, LocalHost, LocalHostIPv6, Broadcast, ..., Any } |
| QHostAddress () | |
| QHostAddress (quint32 ip4Addr ) | |
| QHostAddress (quint8 * ip6Addr ) | |
| QHostAddress (const quint8 * ip6Addr ) | |
| QHostAddress (const Q_IPV6ADDR & ip6Addr ) | |
| QHostAddress (const sockaddr * sockaddr ) | |
| QHostAddress (const QString & address ) | |
| QHostAddress (const QHostAddress & address ) | |
| QHostAddress (QHostAddress::SpecialAddress address ) | |
| ~QHostAddress () | |
| void | clear () |
| bool | isBroadcast () const |
| bool | isEqual (const QHostAddress & other , QHostAddress::ConversionMode mode = TolerantConversion) const |
| bool | isGlobal () const |
| bool | isInSubnet (const QHostAddress & subnet , int netmask ) const |
| bool | isInSubnet (const QPair<QHostAddress, int> & subnet ) const |
| bool | isLinkLocal () const |
| bool | isLoopback () const |
| bool | isMulticast () const |
| bool | isNull () const |
| bool | isSiteLocal () const |
| bool | isUniqueLocalUnicast () const |
| QAbstractSocket::NetworkLayerProtocol | protocol () const |
| QString | scopeId () const |
| void | setAddress (quint32 ip4Addr ) |
| void | setAddress (quint8 * ip6Addr ) |
| void | setAddress (const quint8 * ip6Addr ) |
| void | setAddress (const Q_IPV6ADDR & ip6Addr ) |
| void | setAddress (const sockaddr * sockaddr ) |
| bool | setAddress (const QString & address ) |
| void | setAddress (QHostAddress::SpecialAddress address ) |
| void | setScopeId (const QString & id ) |
| void | swap (QHostAddress & other ) |
| quint32 | toIPv4Address () const |
| quint32 | toIPv4Address (bool * ok ) const |
| Q_IPV6ADDR | toIPv6Address () const |
| QString | toString () const |
| bool | operator!= (const QHostAddress & other ) const |
| bool | operator!= (QHostAddress::SpecialAddress other ) const |
| QHostAddress & | operator= (QHostAddress && other ) |
| QHostAddress & | operator= (const QHostAddress & address ) |
| QHostAddress & | operator= (const QString & address ) |
| QHostAddress & | operator= (QHostAddress::SpecialAddress address ) |
| bool | operator== (const QHostAddress & other ) const |
| bool | operator== (QHostAddress::SpecialAddress other ) const |
| QPair<QHostAddress, int> | parseSubnet (const QString & subnet ) |
| QExplicitlySharedDataPointer<QHostAddressPrivate> | d |
| uint | qHash (const QHostAddress & key , uint seed = 0) |
| bool | operator!= (QHostAddress::SpecialAddress lhs , const QHostAddress & rhs ) |
| QDataStream & | operator<< (QDataStream & out , const QHostAddress & address ) |
| bool | operator== (QHostAddress::SpecialAddress lhs , const QHostAddress & rhs ) |
| 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 .
| 常量 | 值 | 描述 |
|---|---|---|
QHostAddress::StrictConversion
|
0
|
不轉換 IPv6 地址為 IPv4,當比較 2 QHostAddress 不同協議對象,因此它們始終被認為是不同的。 |
QHostAddress::ConvertV4MappedToIPv4
|
1
|
轉換 IPv4 映射 IPv6 地址 (RFC 4291 第 2.5.5.2 章節) 當比較時。因此 QHostAddress ("::ffff:192.168.1.1") 比較等於 QHostAddress ("192.168.1.1")。 |
QHostAddress::ConvertV4CompatToIPv4
|
2
|
轉換 IPv4 兼容 IPv6 地址 (RFC 4291 第 2.5.5.1 章節) 當比較時。因此 QHostAddress ("::192.168.1.1") 比較等於 QHostAddress ("192.168.1.1")。 |
QHostAddress::ConvertLocalHost
|
8
|
轉換 IPv6 迴送地址為其等效 IPv4 當比較時。因此如 QHostAddress ("::1") 比較等於 QHostAddress ("127.0.0.1")。 |
QHostAddress::ConvertUnspecifiedAddress
|
4
|
所有未指定地址比較相等,也就是 AnyIPv4 , AnyIPv6 和任何。 |
QHostAddress::TolerantConversion
|
0xff
|
設置之前的所有 3 標誌。 |
該枚舉在 Qt 5.8 引入或被修改。
ConversionMode 類型是 typedef 對於 QFlags <ConversionModeFlag>。它存儲 ConversionModeFlag 值的 OR (或) 組閤。
另請參閱 isEqual ().
| 常量 | 值 | 描述 |
|---|---|---|
QHostAddress::Null
|
0
|
null 地址對象。相當於 QHostAddress ()。另請參閱 QHostAddress::isNull (). |
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::AnyIPv4
|
6
|
IPv4 任意地址。相當於 QHostAddress ("0.0.0.0")。與此地址綁定的套接字將隻監聽 IPv4 接口。 |
QHostAddress::AnyIPv6
|
5
|
IPv6 任意地址。相當於 QHostAddress ("::"). A socket bound with this address will listen only on IPv6 interfaces. |
QHostAddress::Any
|
4
|
雙堆棧任意地址。與此地址綁定的套接字將監聽 IPv4 和 IPv6 接口。 |
構造 null 主機地址對象,即:對任何主機或接口無效的地址。
另請參閱 clear ().
構造主機地址對象采用 IPv4 地址 ip4Addr .
構造主機地址對象采用 IPv6 地址 ip6Addr .
ip6Addr 必須是網絡字節序 (大端在前) 的 16 字節數組。
構造主機地址對象采用 IPv6 地址 ip6Addr .
ip6Addr 必須是網絡字節序 (大端在前) 的 16 字節數組。
該函數在 Qt 5.5 引入。
構造主機地址對象采用 IPv6 地址 ip6Addr .
構造 IPv4 或 IPv6 地址使用指定地址通過本機結構 sockaddr .
另請參閱 setAddress ().
構造 IPv4 或 IPv6 地址基於字符串 address (如 127.0.0.1)。
另請參閱 setAddress ().
構造副本為給定 address .
構造 QHostAddress 對象為 address .
銷毀主機地址對象。
把主機地址設為 null 並把協議設為 QAbstractSocket::UnknownNetworkLayerProtocol .
另請參閱 QHostAddress::Null .
返迴
true
若地址是 IPv4 廣播地址,
false
否則。IPv4 廣播地址為 255.255.255.255。
注意,此函數不會返迴 true 對於 IPv4 網絡的本地廣播地址。為此,請使用 QNetworkInterface 獲取本地機器的廣播地址。
該函數在 Qt 5.11 引入。
另請參閱 isLoopback (), isGlobal (), isMulticast (), isLinkLocal (),和 isUniqueLocalUnicast ().
返迴
true
若此主機地址如同
other
給定地址;否則返迴
false
.
參數
mode
controls which conversions are preformed between addresses of differing protocols. If no
mode
有給定,
TolerantConversion
默認履行。
該函數在 Qt 5.8 引入。
另請參閱 ConversionMode and operator== ().
返迴
true
若地址是 IPv4 或 IPv6 全局地址,
false
otherwise. A global address is an address that is not reserved for special purposes (like loopback or multicast) or future purposes.
注意,IPv6 唯一本地單播地址被視為是全局地址 (見 isUniqueLocalUnicast ()),作為用於本地網絡的 IPv4 保留地址通過 RFC 1918 .
Also note that IPv6 site-local addresses are deprecated and should be considered as global in new applications. This function returns true for site-local addresses too.
該函數在 Qt 5.11 引入。
另請參閱 isLoopback (), isSiteLocal (),和 isUniqueLocalUnicast ().
返迴
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 ().
這是重載函數。
返迴
true
若此 IP 在子網中描述由
subnet
。
QHostAddress
member of
subnet
contains the network prefix and the int (second) member contains the netmask (prefix length).
該函數在 Qt 4.5 引入。
返迴
true
若地址是 IPv4 或 IPv6 本地鏈接地址,
false
否則。
An IPv4 link-local address is an address in the network 169.254.0.0/16. An IPv6 link-local address is one in the network fe80::/10. See the IANA IPv6 Address Space registry for more information.
該函數在 Qt 5.11 引入。
另請參閱 isLoopback (), isGlobal (), isMulticast (), isSiteLocal (),和 isUniqueLocalUnicast ().
返迴
true
若地址是 IPv6 迴環地址或任何 IPv4 迴環地址。
該函數在 Qt 5.0 引入。
返迴
true
若地址是 IPv4 或 IPv6 多播地址,
false
否則。
該函數在 Qt 5.6 引入。
另請參閱 isLoopback (), isGlobal (), isLinkLocal (), isSiteLocal (),和 isUniqueLocalUnicast ().
返迴
true
若此主機地址無效,對於任何主機或接口。
默認構造函數創建 null 地址。
另請參閱 QHostAddress::Null .
返迴
true
若地址是 IPv6 本地站點地址,
false
否則。
An IPv6 site-local address is one in the network fec0::/10. See the IANA IPv6 Address Space registry for more information.
IPv6 site-local addresses are deprecated and should not be depended upon in new applications. New applications should not depend on this function and should consider site-local addresses the same as global (which is why isGlobal () also returns true). Site-local addresses were replaced by Unique Local Addresses (ULA).
該函數在 Qt 5.11 引入。
另請參閱 isLoopback (), isGlobal (), isMulticast (), isLinkLocal (),和 isUniqueLocalUnicast ().
返迴
true
if the address is an IPv6 unique local unicast address,
false
否則。
An IPv6 unique local unicast address is one in the network fc00::/7. See the IANA IPv6 Address Space registry for more information.
Note that Unique local unicast addresses count as global addresses too. RFC 4193 says that, in practice, "applications may treat these addresses like global scoped addresses." Only routers need care about the distinction.
該函數在 Qt 5.11 引入。
另請參閱 isLoopback (), isGlobal (), isMulticast (), isLinkLocal (), and isUniqueLocalUnicast().
[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 (/).
此函數支持的自變量形式:
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 ().
返迴主機地址的網絡層協議。
返迴 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:
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 (), QNetworkInterface ,和 QNetworkInterface::interfaceFromName .
設置 IPv4 地址指定通過 ip4Addr .
這是重載函數。
設置 IPv6 地址指定通過 ip6Addr .
ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).
這是重載函數。
設置 IPv6 地址指定通過 ip6Addr .
ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).
該函數在 Qt 5.5 引入。
這是重載函數。
設置 IPv6 地址指定通過 ip6Addr .
這是重載函數。
Sets the IPv4 or IPv6 address specified by the native structure
sockaddr
。返迴
true
and sets the address if the address was successfully parsed; otherwise returns
false
.
這是重載函數。
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
.
這是重載函數。
Sets the special address specified by address .
該函數在 Qt 5.8 引入。
Sets the IPv6 scope ID of the address to id . If the address protocol is not IPv6, this function does nothing. The scope ID may be set as an interface name (such as "eth0" or "en1") or as an integer representing the interface index. If id is an interface name, QtNetwork will convert to an interface index using QNetworkInterface::interfaceIndexFromName () before calling the operating system networking functions.
該函數在 Qt 4.1 引入。
另請參閱 scopeId (), QNetworkInterface ,和 QNetworkInterface::interfaceFromName .
交換此主機地址采用 other 。此操作很快且從不失敗。
該函數在 Qt 5.6 引入。
以數字形式返迴 IPv4 地址。
例如:若地址為 127.0.0.1,返迴值為 2130706433 (即 0x7f000001)。
此值有效若 protocol () 是 IPv4Protocol ,或者若協議是 IPv6Protocol ,且 IPv6 地址是 IPv4 映射地址 (RFC4291)。
另請參閱 toString ().
以數字形式返迴 IPv4 地址。
例如:若地址為 127.0.0.1,返迴值為 2130706433 (即 0x7f000001)。
此值有效若 protocol () 是 IPv4Protocol ,或者若協議是 IPv6Protocol , 且 IPv6 地址是 IPv4 映射地址 (RFC4291)。在此情況下, ok 會被設為 true。否則,會被設為 false。
另請參閱 toString ().
以 Q_IPV6ADDR 結構形式返迴 IPv6 地址。結構由 16 個無符號字符組成。
Q_IPV6ADDR addr = hostAddr.toIPv6Address(); // addr contains 16 unsigned characters for (int i = 0; i < 16; ++i) { // process addr[i] }
此值有效若 protocol () 是 IPv6Protocol 。若協議是 IPv4Protocol ,則返迴映射 IPv4 的 IPv6 地址 (RFC4291)。
另請參閱 toString ().
以字符串形式返迴地址。
例如,若地址是 IPv4 地址 127.0.0.1,返迴字符串 127.0.0.1。對於 IPv6,字符串格式將遵循 RFC5952 推薦。對於 QHostAddress::Any ,其 IPv4 地址將返迴 0.0.0.0
另請參閱 toIPv4Address ().
返迴
true
若此主機地址不同於
other
給定地址;否則返迴
false
.
該函數在 Qt 4.2 引入。
返迴
true
若此主機地址不同於
other
給定地址;否則返迴
false
.
移動賦值運算符。
賦值另一主機 address 到此對象,並返迴此對象的引用。
賦值主機地址 address 到此對象,並返迴此對象的引用。
另請參閱 setAddress ().
賦值特殊地址 address 到此對象,並返迴此對象的引用。
該函數在 Qt 5.8 引入。
另請參閱 setAddress ().
返迴
true
若此主機地址如同
other
給定地址;否則返迴
false
. This operator just calls
isEqual
(other,
StrictConversion
).
另請參閱 isEqual ().
返迴
true
若此主機地址如同
other
給定地址;否則返迴
false
.
返迴哈希主機地址 key ,使用 seed 做計算種子。
該函數在 Qt 5.0 引入。
返迴
false
若特殊地址
lhs
如同主機地址
rhs
;否則返迴
true
.
該函數在 Qt 5.9 引入。
另請參閱 isEqual ().
寫入主機地址 address 到流 out 並返迴流引用。
另請參閱 序列化 Qt 數據類型 .
返迴
true
若特殊地址
lhs
如同主機地址
rhs
;否則返迴
false
.
另請參閱 isEqual ().
把主機地址讀入 address 從流 in 並返迴流引用。
另請參閱 序列化 Qt 數據類型 .