IPAddress 66.249.68.9 ("dig -x"는 올바르게 "crawl-66-249-68-9.googlebot.com"라고 표시 함) my lovely 프로그램이 ip68-9-0-0.ri.ri.cox.net이라고 주장하고 있습니다.getnameinfo() - 역방향 DNS 조회 (IP 주소를 호스트 이름으로) C/C++
아래 코드와 비슷한 방식으로 작성하면 마음에 들지 않습니다. IPV4 IP 주소로 시작하고 호스트 이름으로 바꿀 수 있기를 바랍니다.
보너스 질문, 서비스는 무엇입니까? 나는 그것을 필요로하지 않을 것이라고 생각하고, getnameinfo NULL을 줄 것이다. 그러나이 프로그램은 serviceBuffer에 대해 "17145"를 출력하고 있습니다.
#include <string>
#include <iostream>
using namespace std;
#include <netdb.h> // struct sockaddr, AF_INET, NI_NAMEREQD, getnameinfo
#include <string.h> // memset
#include <arpa/inet.h> // inet_pton
int main() {
const string IPAddress { "66.249.68.9" };
struct sockaddr structSockAddr;
memset(&structSockAddr, 0, sizeof(structSockAddr));
structSockAddr.sa_family = AF_INET;
int inetPtonReturnValue { inet_pton(AF_INET, IPAddress.c_str(), &structSockAddr.sa_data) };
if(1 != inetPtonReturnValue) {
cout << "inetPtonReturnValue : " << inetPtonReturnValue << endl; // 0 = src doesn't contain valid address, -1 = af isn't a valid family
}
char hostBuffer[10000];
char serviceBuffer[1000];
int getNameInfoReturnValue { getnameinfo(&structSockAddr, sizeof(structSockAddr), hostBuffer, sizeof(hostBuffer), serviceBuffer, sizeof(serviceBuffer), NI_NAMEREQD) };
if(0 != getNameInfoReturnValue) {
cout << "getNameInfoReturnValue : " << getNameInfoReturnValue << endl
<< "gai_strerror() : " << gai_strerror(getNameInfoReturnValue) << endl;
} else {
cout << "IPAddress : " << IPAddress << endl
<< "hostBuffer : " << hostBuffer << endl
<< "serviceBuffer : " << serviceBuffer << endl;
}
}
'inetPtonReturnValue {inet_pton (AF_INET, IPAddress.c_str(), structSockAddr.sa_data)}';'inet_pton'을'inetPtonReturnValue'에 실제로 할당하는 이유가 스타일 포인트입니까? – Joe
실제 ("보너스"가 아닌) 질문은 무엇입니까? –
Joe - 중괄호 초기화를 선호합니다. – user1902689