Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QHostInfo Class Reference

The QHostInfo class provides static functions for host name lookups. More...

 #include <QHostInfo>

Note: All functions in this class are reentrant.

Public Types

enum HostInfoError { NoError, HostNotFound, UnknownError }

Public Functions

QHostInfo ( int id = -1 )
QHostInfo ( const QHostInfo & other )
~QHostInfo ()
QList<QHostAddress> addresses () const
HostInfoError error () const
QString errorString () const
QString hostName () const
int lookupId () const
void setAddresses ( const QList<QHostAddress> & addresses )
void setError ( HostInfoError error )
void setErrorString ( const QString & str )
void setHostName ( const QString & hostName )
void setLookupId ( int id )
QHostInfo & operator= ( const QHostInfo & other )

Static Public Members

void abortHostLookup ( int id )
QHostInfo fromName ( const QString & name )
QString localDomainName ()
QString localHostName ()
int lookupHost ( const QString & name, QObject * receiver, const char * member )

Detailed Description

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.

Example:

 // To find the IP address of qt.nokia.com
 QHostInfo::lookupHost("qt.nokia.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 Qt for Embedded Linux and disabled multithreading 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("qt.nokia.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.

Note: Since Qt 4.6.1 QHostInfo is using multiple threads for DNS lookup instead of one dedicated DNS thread. This improves performance, but also changes the order of signal emissions when using lookupHost() compared to previous versions of Qt. Note: Since Qt 4.6.3 QHostInfo is using a small internal 60 second DNS cache for performance improvements.

See also QAbstractSocket and RFC 3492.

Member Type Documentation

enum QHostInfo::HostInfoError

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

ConstantValueDescription
QHostInfo::NoError0The lookup was successful.
QHostInfo::HostNotFound1No IP addresses were found for the host.
QHostInfo::UnknownError2An unknown error occurred.

See also error() and setError().

Member Function Documentation

QHostInfo::QHostInfo ( int id = -1 )

Constructs an empty host info object with lookup ID id.

See also lookupId().

QHostInfo::QHostInfo ( const QHostInfo & other )

Constructs a copy of 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.

Example:

 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::localDomainName () [static]

Returns the DNS domain of this machine.

Note: DNS domains are not related to domain names found in Windows networks.

See also hostName().

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 &host)
 {
     if (host.error() != QHostInfo::NoError) {
         qDebug() << "Lookup failed:" << host.errorString();
         return;
     }

     foreach (const 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. Example:

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

Note: There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost().

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.

See also error() and 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.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 78
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Quarterly au hasard

Logo

Les aventures de QLALR - utiliser QLALR pour générer un parseur pour une aventure textuelle

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.7-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web