Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QHostAddress Class

TheQHostAddress class provides an IP address.More...

Header:#include <QHostAddress>

Public Types

enumSpecialAddress { Null, LocalHost, LocalHostIPv6, Broadcast, Any, AnyIPv6 }

Public Functions

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()
voidclear()
boolisInSubnet(const QHostAddress & subnet, int netmask) const
boolisInSubnet(const QPair<QHostAddress, int> & subnet) const
boolisNull() const
QAbstractSocket::NetworkLayerProtocolprotocol() const
QStringscopeId() const
voidsetAddress(quint32 ip4Addr)
voidsetAddress(quint8 * ip6Addr)
voidsetAddress(const Q_IPV6ADDR & ip6Addr)
boolsetAddress(const QString & address)
voidsetAddress(const sockaddr * sockaddr)
voidsetScopeId(const QString & id)
quint32toIPv4Address() const
Q_IPV6ADDRtoIPv6Address() const
QStringtoString() const
booloperator!=(const QHostAddress & other) const
booloperator!=(SpecialAddress other) const
QHostAddress &operator=(const QHostAddress & address)
QHostAddress &operator=(const QString & address)
booloperator==(const QHostAddress & other) const
booloperator==(SpecialAddress other) const

Static Public Members

QPair<QHostAddress, int>parseSubnet(const QString & subnet)

Related Non-Members

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

Detailed Description

TheQHostAddress class provides an IP address.

This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner.

QHostAddress is normally used with theQTcpSocket,QTcpServer, andQUdpSocket to connect to a host or to set up a server.

A host address is set withsetAddress(), and retrieved withtoIPv4Address(),toIPv6Address(), ortoString(). You can check the type withprotocol().

Note:Please note thatQHostAddress does not do DNS lookups.QHostInfo is needed for that.

The class also supports common predefined addresses:Null,LocalHost,LocalHostIPv6,Broadcast, andAny.

See alsoQHostInfo,QTcpSocket,QTcpServer, andQUdpSocket.

Member Type Documentation

enum QHostAddress::SpecialAddress

ConstantValueDescription
QHostAddress::Null0The null address object. Equivalent toQHostAddress().
QHostAddress::LocalHost2The IPv4 localhost address. Equivalent toQHostAddress("127.0.0.1").
QHostAddress::LocalHostIPv63The IPv6 localhost address. Equivalent toQHostAddress("::1").
QHostAddress::Broadcast1The IPv4 broadcast address. Equivalent toQHostAddress("255.255.255.255").
QHostAddress::Any4The IPv4 any-address. Equivalent toQHostAddress("0.0.0.0").
QHostAddress::AnyIPv65The IPv6 any-address. Equivalent toQHostAddress("::").

Member Function Documentation

QHostAddress::QHostAddress()

Constructs a host address object with the IP address 0.0.0.0.

See alsoclear().

QHostAddress::QHostAddress(quint32 ip4Addr)

Constructs a host address object with the IPv4 addressip4Addr.

QHostAddress::QHostAddress(quint8 * ip6Addr)

Constructs a host address object with the IPv6 addressip6Addr.

ip6Addr must be a 16-byte array in network byte order (big endian).

QHostAddress::QHostAddress(constQ_IPV6ADDR & ip6Addr)

Constructs a host address object with the IPv6 addressip6Addr.

QHostAddress::QHostAddress(constsockaddr * sockaddr)

Constructs an IPv4 or IPv6 address using the address specified by the native structuresockaddr.

See alsosetAddress().

QHostAddress::QHostAddress(constQString & address)

Constructs an IPv4 or IPv6 address based on the stringaddress (e.g., "127.0.0.1").

See alsosetAddress().

QHostAddress::QHostAddress(constQHostAddress & address)

Constructs a copy of the givenaddress.

QHostAddress::QHostAddress(SpecialAddress address)

Constructs aQHostAddress object foraddress.

QHostAddress::~QHostAddress()

Destroys the host address object.

void QHostAddress::clear()

Sets the host address to 0.0.0.0.

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

Returns true if this IP is in the subnet described by the network prefixsubnet and netmasknetmask.

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.

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

This function was introduced in Qt 4.5.

See alsoparseSubnet().

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

This is an overloaded function.

Returns true if this IP is in the subnet described bysubnet. TheQHostAddress member ofsubnet contains the network prefix and the int (second) member contains the netmask (prefix length).

This function was introduced in 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(constQString & subnet)

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

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

This function supports arguments in the form:

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

This function was introduced in Qt 4.5.

See alsoisInSubnet().

QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const

Returns the network layer protocol of the host address.

QString QHostAddress::scopeId() const

Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the address does not contain a scope ID, an emptyQString is returned.

The IPv6 scope ID specifies the scope ofreachability 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").

This function was introduced in Qt 4.1.

See alsosetScopeId().

void QHostAddress::setAddress(quint32 ip4Addr)

Set the IPv4 address specified byip4Addr.

void QHostAddress::setAddress(quint8 * ip6Addr)

This is an overloaded function.

Set the IPv6 address specified byip6Addr.

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

void QHostAddress::setAddress(constQ_IPV6ADDR & ip6Addr)

This is an overloaded function.

Set the IPv6 address specified byip6Addr.

bool QHostAddress::setAddress(constQString & address)

This is an overloaded function.

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

void QHostAddress::setAddress(constsockaddr * sockaddr)

This is an overloaded function.

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

void QHostAddress::setScopeId(constQString & id)

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

This function was introduced in Qt 4.1.

See alsoscopeId().

quint32 QHostAddress::toIPv4Address() const

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 the Protocol() isIPv4Protocol.

See alsotoString().

Q_IPV6ADDR QHostAddress::toIPv6Address() const

Returns the IPv6 address as a Q_IPV6ADDR structure. The structure consists of 16 unsigned characters.

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

This value is only valid if theprotocol() isIPv6Protocol.

See alsotoString().

QString QHostAddress::toString() const

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". For IPv6 the string format will follow the RFC5952 recommendation.

See alsotoIPv4Address().

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

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

This function was introduced in Qt 4.2.

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

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

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

Assigns another hostaddress to this object, and returns a reference to this object.

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

Assigns the host addressaddress to this object, and returns a reference to this object.

See alsosetAddress().

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

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

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

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

Related Non-Members

QDataStream &operator<<(QDataStream & out, constQHostAddress & address)

Writes host addressaddress to the streamout and returns a reference to the stream.

See alsoSerializing Qt Data Types.

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

Reads a host address intoaddress from the streamin and returns a reference to the stream.

See alsoSerializing Qt Data Types.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp