📄 qhostaddress.cpp
字号:
to this object.*/QHostAddress &QHostAddress::operator=(const QHostAddress &address){ *d = *address.d; return *this;}/*! Assigns the host address \a address to this object, and returns a reference to this object. \sa setAddress()*/QHostAddress &QHostAddress::operator=(const QString &address){ setAddress(address); return *this;}/*! \fn bool QHostAddress::operator!=(const QHostAddress &other) const \since 4.2 Returns true if this host address is not the same as the \a other address given; otherwise returns false.*//*! \fn bool QHostAddress::operator!=(SpecialAddress other) const Returns true if this host address is not the same as the \a other address given; otherwise returns false.*//*! Sets the host address to 0.0.0.0.*/void QHostAddress::clear(){ d->clear();}/*! Set the IPv4 address specified by \a ip4Addr.*/void QHostAddress::setAddress(quint32 ip4Addr){ d->setAddress(ip4Addr);}/*! \overload Set the IPv6 address specified by \a ip6Addr. \a ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).*/void QHostAddress::setAddress(quint8 *ip6Addr){ d->setAddress(ip6Addr);}/*! \overload Set the IPv6 address specified by \a ip6Addr.*/void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr){ d->setAddress(ip6Addr);}/*! \overload Sets the IPv4 or IPv6 address specified by the string representation specified by \a address (e.g. "127.0.0.1"). Returns true and sets the address if the address was successfully parsed; otherwise returns false.*/bool QHostAddress::setAddress(const QString &address){ d->ipString = address; return d->parse();}/*! \fn void QHostAddress::setAddress(const sockaddr *sockaddr) \overload Sets the IPv4 or IPv6 address specified by the native structure \a sockaddr. Returns true and sets the address if the address was successfully parsed; otherwise returns false.*/void QHostAddress::setAddress(const struct sockaddr *sockaddr){ clear(); if (sockaddr->sa_family == AF_INET) setAddress(htonl(((sockaddr_in *)sockaddr)->sin_addr.s_addr));#ifndef QT_NO_IPV6 else if (sockaddr->sa_family == AF_INET6) setAddress(((qt_sockaddr_in6 *)sockaddr)->sin6_addr.qt_s6_addr);#endif}/*! Returns the IPv4 address as a number. For example, if the address is 127.0.0.1, the returned value is 2130706433 (i.e. 0x7f000001). This value is only valid if isIp4Addr() returns true. \sa toString()*/quint32 QHostAddress::toIPv4Address() const{ QT_ENSURE_PARSED(this); return d->a;}/*! Returns the network layer protocol of the host address.*/QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const{ QT_ENSURE_PARSED(this); return d->protocol;}/*! Returns the IPv6 address as a Q_IPV6ADDR structure. The structure consists of 16 unsigned characters. \code Q_IPV6ADDR addr = hostAddr.toIPv6Address(); // addr contains 16 unsigned characters for (int i = 0; i < 16; ++i) { // process addr[i] } \endcode This value is only valid if isIPv6Address() returns true. \sa toString()*/Q_IPV6ADDR QHostAddress::toIPv6Address() const{ QT_ENSURE_PARSED(this); return d->a6;}/*! Returns the address as a string. For example, if the address is the IPv4 address 127.0.0.1, the returned string is "127.0.0.1". \sa toIPv4Address()*/QString QHostAddress::toString() const{ QT_ENSURE_PARSED(this); if (d->protocol == QAbstractSocket::IPv4Protocol) { quint32 i = toIPv4Address(); QString s; s.sprintf("%d.%d.%d.%d", (i>>24) & 0xff, (i>>16) & 0xff, (i >> 8) & 0xff, i & 0xff); return s; } if (d->protocol == QAbstractSocket::IPv6Protocol) { quint16 ugle[8]; for (int i = 0; i < 8; i++) { ugle[i] = (quint16(d->a6[2*i]) << 8) | quint16(d->a6[2*i+1]); } QString s; s.sprintf("%X:%X:%X:%X:%X:%X:%X:%X", ugle[0], ugle[1], ugle[2], ugle[3], ugle[4], ugle[5], ugle[6], ugle[7]); if (!d->scopeId.isEmpty()) s.append(QLatin1Char('%') + d->scopeId); return s; } return QString();}/*! \since 4.1 Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the address does not contain a scope ID, an empty QString is returned. The IPv6 scope ID specifies the scope of \e 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: \list \o Node-local: Addresses that are only used for communicating with services on the same interface (e.g., the loopback interface "::1"). \o Link-local: Addresses that are local to the network interface (\e{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. \o 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. \o Global: For globally routable addresses, such as public servers on the Internet. \endlist 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"). \sa setScopeId()*/QString QHostAddress::scopeId() const{ QT_ENSURE_PARSED(this); return (d->protocol == QAbstractSocket::IPv6Protocol) ? d->scopeId : QString();}/*! \since 4.1 Sets the IPv6 scope ID of the address to \a id. If the address protocol is not IPv6, this function does nothing.*/void QHostAddress::setScopeId(const QString &id){ QT_ENSURE_PARSED(this); if (d->protocol == QAbstractSocket::IPv6Protocol) d->scopeId = id;}/*! Returns true if this host address is the same as the \a other address given; otherwise returns false.*/bool QHostAddress::operator==(const QHostAddress &other) const{ QT_ENSURE_PARSED(this); QT_ENSURE_PARSED(&other); if (d->protocol == QAbstractSocket::IPv4Protocol) return other.d->protocol == QAbstractSocket::IPv4Protocol && d->a == other.d->a; if (d->protocol == QAbstractSocket::IPv6Protocol) { return other.d->protocol == QAbstractSocket::IPv6Protocol && memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0; } return d->protocol == other.d->protocol;}/*! Returns true if this host address is the same as the \a other address given; otherwise returns false.*/bool QHostAddress::operator ==(SpecialAddress other) const{ QT_ENSURE_PARSED(this); QHostAddress otherAddress(other); QT_ENSURE_PARSED(&otherAddress); if (d->protocol == QAbstractSocket::IPv4Protocol) return otherAddress.d->protocol == QAbstractSocket::IPv4Protocol && d->a == otherAddress.d->a; if (d->protocol == QAbstractSocket::IPv6Protocol) { return otherAddress.d->protocol == QAbstractSocket::IPv6Protocol && memcmp(&d->a6, &otherAddress.d->a6, sizeof(Q_IPV6ADDR)) == 0; } return int(other) == int(Null);}/*! 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.*/bool QHostAddress::isNull() const{ QT_ENSURE_PARSED(this); return d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol;}/*! \fn quint32 QHostAddress::ip4Addr() const Use toIPv4Address() instead.*//*! \fn bool QHostAddress::isIp4Addr() const Use protocol() instead.*//*! \fn bool QHostAddress::isIPv4Address() const Use protocol() instead.*//*! \fn bool QHostAddress::isIPv6Address() const Use protocol() instead.*/#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug d, const QHostAddress &address){ d.maybeSpace() << "QHostAddress(" << address.toString() << ")"; return d.space();}#endifuint qHash(const QHostAddress &key){ return qHash(key.toString());}#ifndef QT_NO_DATASTREAM/*! \relates QHostAddress Writes host address \a address to the stream \a out and returns a reference to the stream. \sa {Format of the QDataStream operators}*/QDataStream &operator<<(QDataStream &out, const QHostAddress &address){ qint8 prot; prot = qint8(address.protocol()); out << prot; switch (address.protocol()) { case QAbstractSocket::UnknownNetworkLayerProtocol: break; case QAbstractSocket::IPv4Protocol: out << address.toIPv4Address(); break; case QAbstractSocket::IPv6Protocol: { Q_IPV6ADDR ipv6 = address.toIPv6Address(); for (int i = 0; i < 16; ++i) out << ipv6[i]; out << address.scopeId(); } break; } return out;}/*! \relates QHostAddress Reads a host address into \a address from the stream \a in and returns a reference to the stream. \sa {Format of the QDataStream operators}*/QDataStream &operator>>(QDataStream &in, QHostAddress &address){ qint8 prot; in >> prot; switch (QAbstractSocket::NetworkLayerProtocol(prot)) { case QAbstractSocket::UnknownNetworkLayerProtocol: address.clear(); break; case QAbstractSocket::IPv4Protocol: { quint32 ipv4; in >> ipv4; address.setAddress(ipv4); } break; case QAbstractSocket::IPv6Protocol: { Q_IPV6ADDR ipv6; for (int i = 0; i < 16; ++i) in >> ipv6[i]; address.setAddress(ipv6); QString scope; in >> scope; address.setScopeId(scope); } break; default: address.clear(); in.setStatus(QDataStream::ReadCorruptData); } return in;}#endif //QT_NO_DATASTREAM
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -