QHostAddress 类

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 (SpecialAddress address )
~QHostAddress ()
void clear ()
bool isEqual (const QHostAddress & other , ConversionMode mode = TolerantConversion) const
bool isInSubnet (const QHostAddress & subnet , int netmask ) const
bool isInSubnet (const QPair<QHostAddress, int> & subnet ) const
bool isLoopback () const
bool isMulticast () const
bool isNull () 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 (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!= (SpecialAddress other ) const
QHostAddress & operator= (QHostAddress && other )
QHostAddress & operator= (const QHostAddress & address )
QHostAddress & operator= (const QString & address )
QHostAddress & operator= (SpecialAddress address )
bool operator== (const QHostAddress & other ) const
bool operator== (SpecialAddress other ) const

静态公共成员

QPair<QHostAddress, int> parseSubnet (const QString & subnet )
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 )

详细描述

QHostAddress 类提供 IP 地址。

此类以与平台和协议无关的方式保持 IPv4 或 IPv6 地址。

QHostAddress 通常用于 QTcpSocket , QTcpServer ,和 QUdpSocket 以连接到主机或设置服务器。

设置主机地址采用 setAddress (),和检索采有 toIPv4Address (), toIPv6Address (),或 toString ()。可以检查类型采用 protocol ().

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

类还支持常见预定义地址: Null , LocalHost , LocalHostIPv6 , Broadcast ,和 任何 .

另请参阅 QHostInfo , QTcpSocket , QTcpServer ,和 QUdpSocket .

成员类型文档编制

enum QHostAddress:: ConversionModeFlag
flags QHostAddress:: ConversionMode

常量 描述
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 ().

enum QHostAddress:: SpecialAddress

常量 描述
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 ("::")。与此地址绑定的套接字将只监听 IPv6 接口。
QHostAddress::Any 4 双堆栈任意地址。与此地址绑定的套接字将监听 IPv4 和 IPv6 接口。

成员函数文档编制

QHostAddress:: QHostAddress ()

构造 null 主机地址对象,即:对任何主机或接口无效的地址。

另请参阅 clear ().

QHostAddress:: QHostAddress ( quint32 ip4Addr )

构造主机地址对象采用 IPv4 地址 ip4Addr .

QHostAddress:: QHostAddress ( quint8 * ip6Addr )

构造主机地址对象采用 IPv6 地址 ip6Addr .

ip6Addr 必须是网络字节序 (大端在前) 的 16 字节数组。

QHostAddress:: QHostAddress (const quint8 * ip6Addr )

构造主机地址对象采用 IPv6 地址 ip6Addr .

ip6Addr 必须是网络字节序 (大端在前) 的 16 字节数组。

该函数在 Qt 5.5 引入。

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 ()

将主机地址设为 null。

另请参阅 QHostAddress::Null .

bool QHostAddress:: isEqual (const QHostAddress & other , ConversionMode mode = TolerantConversion) const

返回 true 若此主机地址如同 other 给定地址;否则返回 false .

参数 mode controls which conversions are preformed between addresses of differing protocols. If no mode 有给定, TolerantConversion 默认履行。

该函数在 Qt 5.8 引入。

另请参阅 ConversionMode and operator== ().

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

返回 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.

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

这是重载函数。

返回 true 若此 IP 在子网中描述由 subnet QHostAddress member of subnet contains the network prefix and the int (second) member contains the netmask (prefix length).

该函数在 Qt 4.5 引入。

bool QHostAddress:: isLoopback () const

返回 true 若地址是 IPv6 回环地址或任何 IPv4 回环地址。

该函数在 Qt 5.0 引入。

bool QHostAddress:: isMulticast () const

返回 true 若地址是 IPv4 或 IPv6 多播地址, false 否则。

该函数在 Qt 5.6 引入。

bool QHostAddress:: isNull () const

返回 true 若此主机地址无效,对于任何主机或接口。

默认构造函数创建 null 地址。

另请参阅 QHostAddress::Null .

[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.
  • 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 (), QNetworkInterface ,和 QNetworkInterface::interfaceFromName .

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 quint8 * ip6Addr )

这是重载函数。

设置 IPv6 地址指定通过 ip6Addr .

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

该函数在 Qt 5.5 引入。

void QHostAddress:: setAddress (const Q_IPV6ADDR & ip6Addr )

这是重载函数。

设置 IPv6 地址指定通过 ip6Addr .

void QHostAddress:: setAddress (const sockaddr * sockaddr )

这是重载函数。

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 .

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 ( SpecialAddress address )

这是重载函数。

Sets the special address specified by address .

该函数在 Qt 5.8 引入。

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. 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 .

void QHostAddress:: swap ( QHostAddress & other )

交换此主机地址采用 other 。此操作非常快且从不失败。

该函数在 Qt 5.6 引入。

quint32 QHostAddress:: toIPv4Address () const

以数字形式返回 IPv4 地址。

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

此值有效若 protocol () 是 IPv4Protocol ,或者若协议是 IPv6Protocol ,且 IPv6 地址是 IPv4 映射地址 (RFC4291)。

另请参阅 toString ().

quint32 QHostAddress:: toIPv4Address ( bool * ok ) const

以数字形式返回 IPv4 地址。

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

此值有效若 protocol () 是 IPv4Protocol ,或者若协议是 IPv6Protocol , 且 IPv6 地址是 IPv4 映射地址 (RFC4291)。在此情况下, ok 会被设为 true。否则,会被设为 false。

另请参阅 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]
}
					

此值有效若 protocol () 是 IPv6Protocol 。若协议是 IPv4Protocol ,则返回映射 IPv4 的 IPv6 地址 (RFC4291)。

另请参阅 toString ().

QString QHostAddress:: toString () const

以字符串形式返回地址。

例如,若地址是 IPv4 地址 127.0.0.1,返回字符串 127.0.0.1。对于 IPv6,字符串格式将遵循 RFC5952 推荐。对于 QHostAddress::Any ,其 IPv4 地址将返回 0.0.0.0

另请参阅 toIPv4Address ().

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

返回 true 若此主机地址不同于 other 给定地址;否则返回 false .

该函数在 Qt 4.2 引入。

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

返回 true 若此主机地址不同于 other 给定地址;否则返回 false .

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

移动赋值运算符。

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

赋值另一主机 address 到此对象,并返回此对象的引用。

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

赋值主机地址 address 到此对象,并返回此对象的引用。

另请参阅 setAddress ().

QHostAddress &QHostAddress:: operator= ( SpecialAddress address )

赋值特殊地址 address 到此对象,并返回此对象的引用。

该函数在 Qt 5.8 引入。

另请参阅 setAddress ().

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

返回 true 若此主机地址如同 other 给定地址;否则返回 false . This operator just calls isEqual (other, StrictConversion ).

另请参阅 isEqual ().

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

返回 true 若此主机地址如同 other 给定地址;否则返回 false .

相关非成员

uint qHash (const QHostAddress & key , uint seed = 0)

返回哈希主机地址 key ,使用 seed 做计算种子。

该函数在 Qt 5.0 引入。

bool operator!= ( QHostAddress::SpecialAddress lhs , const QHostAddress & rhs )

返回 false 若特殊地址 lhs 如同主机地址 rhs ;否则返回 true .

该函数在 Qt 5.9 引入。

另请参阅 isEqual ().

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

写入主机地址 address 到流 out 并返回流引用。

另请参阅 序列化 Qt 数据类型 .

bool operator== ( QHostAddress::SpecialAddress lhs , const QHostAddress & rhs )

返回 true 若特殊地址 lhs 如同主机地址 rhs ;否则返回 false .

另请参阅 isEqual ().

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

把主机地址读入 address 从流 in 并返回流引用。

另请参阅 序列化 Qt 数据类型 .