QHostInfo Class

QHostInfo 클래스는 호스트 이름 조회를 위한 정적 함수를 제공합니다. 더 보기...

Header: #include <QHostInfo>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network

참고: 이 클래스의 모든 함수는 재인용됩니다.

공용 유형

enum HostInfoError { NoError, HostNotFound, UnknownError }

공용 함수

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

정적 공용 멤버

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

상세 설명

QHostInfo는 호스트 이름과 연관된 IP 주소 또는 IP 주소와 연관된 호스트 이름을 찾습니다. 이 클래스는 비동기적으로 작동하고 호스트가 발견되면 신호를 방출하는 함수와 QHostInfo 객체를 차단하고 반환하는 함수의 두 가지 정적 편의 함수를 제공합니다.

호스트의 IP 주소를 비동기적으로 조회하려면 lookupHost()를 호출하면 호스트 이름 또는 IP 주소, 수신자 객체, 슬롯 서명을 인수로 받아 ID를 반환합니다. 조회 ID와 함께 abortHostLookup()를 호출하여 조회를 중단할 수 있습니다.

예시:

// To find the IP address of qt-project.org
QHostInfo::lookupHost("qt-project.org", this, &MyWidget::printResults);

// To find the host name for 4.2.2.1
QHostInfo::lookupHost("4.2.2.1", this, &MyWidget::printResults);

결과가 준비되면 슬롯이 호출됩니다. 결과는 QHostInfo 객체에 저장됩니다. addresses ()를 호출하여 호스트의 IP 주소 목록을 가져오고 hostName()를 호출하여 조회한 호스트 이름을 가져옵니다.

조회에 실패하면 error()는 발생한 오류 유형을 반환합니다. errorString()는 조회 오류에 대한 사람이 읽을 수 있는 설명을 제공합니다.

차단 조회를 원하면 QHostInfo::fromName() 함수를 사용하세요:

QHostInfo info = QHostInfo::fromName("qt-project.org");

QHostInfo는 IDNA 및 Punycode 표준을 통해 국제화된 도메인 네임(IDN)을 지원합니다.

로컬 호스트의 이름을 검색하려면 정적 QHostInfo::localHostName() 함수를 사용합니다.

QHostInfo는 운영 체제에서 제공하는 메커니즘을 사용하여 조회를 수행합니다. RFC 6724에 따라 도메인이나 호스트에 등록된 모든 IP 주소가 반환된다는 보장은 없습니다.

참고: Qt 4.6.1부터 QHostInfo는 하나의 전용 DNS 스레드 대신 여러 개의 스레드를 사용하여 DNS 조회를 수행합니다. 이로 인해 성능이 향상되었지만 lookupHost()를 사용할 때 이전 버전의 Qt에 비해 신호 방출 순서가 변경되었습니다.

참고: Qt 4.6.3부터 QHostInfo는 성능 향상을 위해 작은 내부 60초 DNS 캐시를 사용하고 있습니다.

QAbstractSocket, RFC 3492RFC 6724를참조하십시오 .

멤버 유형 문서

enum QHostInfo::HostInfoError

이 열거형은 호스트 이름을 확인하려고 할 때 발생할 수 있는 다양한 오류를 설명합니다.

Constant설명
QHostInfo::NoError0조회에 성공했습니다.
QHostInfo::HostNotFound1호스트에 대한 IP 주소를 찾을 수 없습니다.
QHostInfo::UnknownError2알 수 없는 오류가 발생했습니다.

error() 및 setError()도 참조하세요 .

멤버 함수 문서

[explicit] QHostInfo::QHostInfo(int id = -1)

조회 ID가 id 인 빈 호스트 정보 객체를 생성합니다.

lookupId()도 참조하세요 .

QHostInfo::QHostInfo(const QHostInfo &other)

other 의 복사본을 생성합니다.

[noexcept] QHostInfo::QHostInfo(QHostInfo &&other)

Move는 other 에서 새 QHostInfo를 생성합니다.

참고: 이동한 개체 other 는 부분적으로 형성된 상태로 배치되며, 유효한 작업은 파괴와 새 값 할당뿐입니다.

[noexcept] QHostInfo::~QHostInfo()

호스트 정보 개체를 삭제합니다.

[static] void QHostInfo::abortHostLookup(int id)

lookupHost()에서 반환한 ID id 로 호스트 조회를 중단합니다.

lookupHost() 및 lookupId()도 참조하세요 .

QList<QHostAddress> QHostInfo::addresses() const

hostName()와 연결된 IP 주소 목록을 반환합니다. 이 목록은 비어 있을 수 있습니다.

예제:

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

setAddresses(), hostName() 및 error()도 참조하세요 .

QHostInfo::HostInfoError QHostInfo::error() const

호스트 이름 조회에 실패한 경우 발생한 오류 유형을 반환하고, 그렇지 않으면 NoError 을 반환합니다.

setError() 및 errorString()도 참조하세요 .

QString QHostInfo::errorString() const

조회에 실패하면 이 함수는 사람이 읽을 수 있는 오류 설명을 반환하고, 그렇지 않으면 "알 수 없는 오류"를 반환합니다.

setErrorString() 및 error()도 참조하세요 .

[static] QHostInfo QHostInfo::fromName(const QString &name)

주어진 호스트 name 의 IP 주소를 조회합니다. 이 함수는 조회하는 동안 블록되므로 조회 결과가 준비될 때까지 프로그램 실행이 일시 중단됩니다. 조회 결과를 QHostInfo 객체로 반환합니다.

호스트 이름 대신 리터럴 IP 주소를 name 에 전달하면 QHostInfo 은 해당 IP의 도메인 이름을 검색합니다(즉, QHostInfo역방향 조회를 수행합니다). 성공하면 반환되는 QHostInfo 에는 호스트 이름의 확인된 도메인 이름과 IP 주소가 모두 포함됩니다.

lookupHost()도 참조하세요 .

QString QHostInfo::hostName() const

IP 주소를 조회한 호스트의 이름을 반환합니다.

setHostName() 및 localHostName()도 참조하세요 .

[static] QString QHostInfo::localDomainName()

이 컴퓨터의 DNS 도메인을 반환합니다.

참고: DNS 도메인은 Windows 네트워크에 있는 도메인 이름과 관련이 없습니다.

hostName()도 참조하세요 .

[static] QString QHostInfo::localHostName()

이 머신의 호스트 이름이 구성된 경우 호스트 이름을 반환합니다. 특히 호스트 이름이 자동으로 구성된 경우에는 호스트 이름이 전 세계적으로 고유하다는 보장이 없다는 점에 유의하세요.

이 함수는 반환된 호스트 이름이 FQDN(정규화된 도메인 이름)임을 보장하지 않습니다. 이 경우 fromName()를 사용하여 반환된 이름을 FQDN으로 확인합니다.

이 함수는 QSysInfo::machineHostName()와 동일한 결과를 반환합니다.

hostName() 및 localDomainName()도 참조하세요 .

[static] int QHostInfo::lookupHost(const QString &name, const QObject *receiver, const char *member)

호스트 이름 name 과 연결된 IP 주소를 조회하고 조회용 ID를 반환합니다. 조회 결과가 준비되면 receiver 의 슬롯 또는 신호 memberQHostInfo 인수를 사용하여 호출됩니다. 그런 다음 QHostInfo 객체를 검사하여 조회 결과를 가져올 수 있습니다.

조회는 예를 들어 단일 함수 호출로 수행됩니다:

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

슬롯의 구현은 조회에서 반환된 주소에 대한 기본 정보를 인쇄하거나 실패한 경우 오류를 보고합니다:

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

호스트 이름 대신 리터럴 IP 주소를 name 에 전달하면 QHostInfo 에서 해당 IP에 대한 도메인 이름을 검색합니다(즉, QHostInfo 에서 역방향 조회를 수행합니다). 성공하면 결과 QHostInfo 에는 호스트 이름에 대한 확인된 도메인 이름과 IP 주소가 모두 포함됩니다. 예시:

QHostInfo::lookupHost("4.2.2.1", this, &MyWidget::lookedUp);

참고: lookupHost()로 여러 요청을 시작하는 경우 신호가 전송되는 순서는 보장되지 않습니다.

참고: 6.7 이전 Qt 버전에서 이 함수는 receiver 를 (비-const) QObject* 로 받습니다.

abortHostLookup(), addresses(), error() 및 fromName()도 참조하세요 .

[static] template <typename Functor> int QHostInfo::lookupHost(const QString &name, Functor &&functor)

이 함수는 오버로드된 함수입니다.

호스트 이름 name 과 연결된 IP 주소를 조회하고 조회용 ID를 반환합니다. 조회 결과가 준비되면 QHostInfo 인수를 사용하여 functor 을 호출합니다. 그런 다음 QHostInfo 객체를 검사하여 조회 결과를 가져올 수 있습니다.

functor 은 lookupHost를 호출하는 스레드에서 실행되며, 해당 스레드에는 실행 중인 Qt 이벤트 루프가 있어야 합니다.

참고: lookupHost()로 여러 요청을 시작하는 경우 신호가 방출되는 순서는 보장할 수 없습니다.

abortHostLookup(), addresses(), error() 및 fromName()도 참조하세요 .

[static] template <typename Functor> int QHostInfo::lookupHost(const QString &name, const QObject *context, Functor functor)

이 함수는 오버로드된 함수입니다.

호스트 이름 name 과 연결된 IP 주소를 조회하고 조회용 ID를 반환합니다. 조회 결과가 준비되면 QHostInfo 인수를 사용하여 functor 함수를 호출합니다. 그런 다음 QHostInfo 객체를 검사하여 조회 결과를 가져올 수 있습니다.

조회가 완료되기 전에 context 가 파괴되면 functor 은 호출되지 않습니다. functorcontext 의 스레드에서 실행됩니다. 컨텍스트의 스레드에는 실행 중인 Qt 이벤트 루프가 있어야 합니다.

다음은 함수에 대한 대체 서명입니다:

lookupHost(const QString &name, const QObject *receiver, PointerToMemberFunction function)

이 경우 조회 결과가 준비되면 receiver 의 슬롯 또는 신호 functionQHostInfo 인수를 사용하여 호출됩니다. 그런 다음 QHostInfo 객체를 검사하여 조회 결과를 가져올 수 있습니다.

참고: lookupHost()로 여러 요청을 시작하는 경우 신호가 방출되는 순서는 보장되지 않습니다.

abortHostLookup(), addresses(), error() 및 fromName()도 참조하세요 .

int QHostInfo::lookupId() const

이 조회의 ID를 반환합니다.

setLookupId(), abortHostLookup() 및 hostName()도 참조하세요 .

void QHostInfo::setAddresses(const QList<QHostAddress> &addresses)

QHostInfo 의 주소 목록을 addresses 으로 설정합니다.

addresses()도 참조하세요 .

void QHostInfo::setError(QHostInfo::HostInfoError error)

QHostInfo 의 오류 유형을 error 으로 설정합니다.

error() 및 errorString()도 참조하세요 .

void QHostInfo::setErrorString(const QString &str)

조회에 실패한 경우 발생한 오류에 대한 사람이 읽을 수 있는 설명을 str 으로 설정합니다.

errorString() 및 setError()도 참조하세요 .

void QHostInfo::setHostName(const QString &hostName)

QHostInfo 의 호스트 이름을 hostName 로 설정합니다.

hostName()도 참조하세요 .

void QHostInfo::setLookupId(int id)

이 조회의 ID를 id 로 설정합니다.

lookupId() 및 lookupHost()도 참조하세요 .

[noexcept] void QHostInfo::swap(QHostInfo &other)

이 호스트 정보를 other 로 바꿉니다. 이 작업은 매우 빠르며 실패하지 않습니다.

[noexcept] QHostInfo &QHostInfo::operator=(QHostInfo &&other)

이동 - other 을 이 QHostInfo 인스턴스에 할당합니다.

참고: 이동된 other 객체는 부분적으로 형성된 상태로 배치되며, 유효한 작업은 소멸과 새 값 할당뿐입니다.

QHostInfo &QHostInfo::operator=(const QHostInfo &other)

other 객체의 데이터를 이 호스트 정보 객체에 할당하고 이에 대한 참조를 반환합니다.

© 2025 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 the GNU 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.