Qt:Документация 4.3.2/qhostinfo

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
40px Внимание: Актуальная версия перевода документации находится здесь

__NOTOC__

Image:qt-logo.png

Главная · Все классы · Основные классы · Классы по группам · Модули · Функции

Image:trolltech-logo.png

Содержание

[править] QHostInfo Class Reference
[модуль QtNetwork ]

The QHostInfo class provides static functions for host name lookups. Далее...

 #include <QHostInfo>

Примечание: все функции в этом классе реентерабельны.

[править] Открытые типы

[править] Открытые функции

[править] Статические открытые члены


[править] Подробное описание

The QHostInfo class provides static functions for host name lookups.

QHostInfo uses the lookup mechanisms provided by the operating system to find the IP address(es) associated with a host name, or the host name associated with an IP address. The class provides two static convenience functions: one that works asynchronously and emits a signal once the host is found, and one that blocks and returns a QHostInfo object.

To look up a host's IP addresses asynchronously, call lookupHost(), which takes the host name or IP address, a receiver object, and a slot signature as arguments and returns an ID. You can abort the lookup by calling abortHostLookup() with the lookup ID.

Пример:

 // To find the IP address of www.trolltech.com
 QHostInfo::lookupHost("www.trolltech.com",
                       this, SLOT(printResults(QHostInfo)));
 
 // To find the host name for 4.2.2.1
 QHostInfo::lookupHost("4.2.2.1",
                       this, SLOT(printResults(QHostInfo)));

The slot is invoked when the results are ready. (If you use Qtopia Core and disabled multithread support by defining QT_NO_THREAD, lookupHost() will block until the lookup has finished.) The results are stored in a QHostInfo object. Call addresses() to get the list of IP addresses for the host, and hostName() to get the host name that was looked up.

If the lookup failed, error() returns the type of error that occurred. errorString() gives a human-readable description of the lookup error.

If you want a blocking lookup, use the QHostInfo::fromName() function:

 QHostInfo info = QHostInfo::fromName("www.trolltech.com");

QHostInfo supports Internationalized Domain Names (IDNs) through the IDNA and Punycode standards.

To retrieve the name of the local host, use the static QHostInfo::localHostName() function.

See also QAbstractSocket and RFC 3492.


[править] Описание типов

[править]
enum QHostInfo::HostInfoError

This enum describes the various errors that can occur when trying to resolve a host name.


Константа Значение Описание
QHostInfo::NoError 0 The lookup was successful.
QHostInfo::HostNotFound 1 No IP addresses were found for the host.
QHostInfo::UnknownError 2 An unknown error occurred.

See also error() and setError().


[править] Описание функций-членов

[править]
QHostInfo::QHostInfo ( int id = -1 )

Constructs an empty host info object with lookup ID id.

See also lookupId().

[править]
QHostInfo::QHostInfo ( const QHostInfo & other )

Создаёт копию other.

[править]
QHostInfo::~QHostInfo ()

Destroys the host info object.

[править]
void QHostInfo::abortHostLookup ( int id ) [static]

Aborts the host lookup with the ID id, as returned by lookupHost().

See also lookupHost() and lookupId().

[править]
QList< QHostAddress> QHostInfo::addresses () const

Returns the list of IP addresses associated with hostName(). This list may be empty.

Пример:

 QHostInfo info;
 ...
 if (!info.addresses().isEmpty()) {
     QHostAddress address = info.addresses().first();
     // use the first IP address
 }

See also setAddresses(), hostName(), and error().

[править]
HostInfoError QHostInfo::error () const

Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError.

See also setError() and errorString().

[править]
QString QHostInfo::errorString () const

If the lookup failed, this function returns a human readable description of the error; otherwise "Unknown error" is returned.

See also setErrorString() and error().

[править]
QHostInfo QHostInfo::fromName ( const QString & name ) [static]

Looks up the IP address(es) for the given host name. The function blocks during the lookup which means that execution of the program is suspended until the results of the lookup are ready. Returns the result of the lookup in a QHostInfo object.

If you pass a literal IP address to name instead of a host name, QHostInfo will search for the domain name for the IP (i.e., QHostInfo will perform a reverse lookup). On success, the returned QHostInfo will contain both the resolved domain name and IP addresses for the host name.

See also lookupHost().

[править]
QString QHostInfo::hostName () const

Returns the name of the host whose IP addresses were looked up.

See also setHostName() and localHostName().

[править]
QString QHostInfo::localHostName () [static]

Returns the host name of this machine.

See also hostName().

[править]
int QHostInfo::lookupHost ( const QString & name, QObject * receiver, const char * member ) [static]

Looks up the IP address(es) associated with host name name, and returns an ID for the lookup. When the result of the lookup is ready, the slot or signal member in receiver is called with a QHostInfo argument. The QHostInfo object can then be inspected to get the results of the lookup.

The lookup is performed by a single function call, for example:

 QHostInfo::lookupHost("www.kde.org",
                       this, SLOT(lookedUp(QHostInfo)));

The implementation of the slot prints basic information about the addresses returned by the lookup, or reports an error if it failed:

 void MyWidget::lookedUp(const QHostInfo &amp;host)
 {
     if (host.error() != QHostInfo::NoError) {
         qDebug() << "Lookup failed:" << host.errorString();
         return;
     }
 
     foreach (QHostAddress address, host.addresses())
         qDebug() << "Found address:" << address.toString();
 }

If you pass a literal IP address to name instead of a host name, QHostInfo will search for the domain name for the IP (i.e., QHostInfo will perform a reverse lookup). On success, the resulting QHostInfo will contain both the resolved domain name and IP addresses for the host name. Пример:

 QHostInfo::lookupHost("4.2.2.1",
                       this, SLOT(lookedUp(QHostInfo)));

See also abortHostLookup(), addresses(), error(), and fromName().

[править]
int QHostInfo::lookupId () const

Returns the ID of this lookup.

See also setLookupId(), abortHostLookup(), and hostName().

[править]
void QHostInfo::setAddresses ( const QList< QHostAddress> & addresses )

Sets the list of addresses in this QHostInfo to addresses.

See also addresses().

[править]
void QHostInfo::setError ( HostInfoError error )

Sets the error type of this QHostInfo to error.

Смотрите также error() и errorString().

[править]
void QHostInfo::setErrorString ( const QString & str )

Sets the human readable description of the error that occurred to str if the lookup failed.

See also errorString() and setError().

[править]
void QHostInfo::setHostName ( const QString & hostName )

Sets the host name of this QHostInfo to hostName.

See also hostName().

[править]
void QHostInfo::setLookupId ( int id )

Sets the ID of this lookup to id.

See also lookupId() and lookupHost().

[править]
QHostInfo & QHostInfo::operator= ( const QHostInfo & other )

Assigns the data of the other object to this host info object, and returns a reference to it.



Copyright © 2007 Trolltech Trademarks
Qt 4.3.2