2010-11-17 3 views
0

Comment obtenir le nom de domaine de Given IP dans MFC (VC++)? Le code que je utilise est comme ci-dessous:Comment obtenir le nom de domaine de Given IP dans MFC (VC++)?

#include "stdafx.h" 
#include <winsock2.h> 
#include <ws2tcpip.h> 
#include <stdio.h> 

// link with ws2_32.lib 
#pragma comment(lib, "Ws2_32.lib") 


int _tmain(int argc, char **argv) 
{ 

    //----------------------------------------- 
    // Declare and initialize variables 
    WSADATA wsaData = {0}; 
    int iResult = 0; 

    DWORD dwRetval; 

    struct sockaddr_in saGNI; 
    char hostname[NI_MAXHOST]; 
char servInfo[NI_MAXSERV]; 
u_short port = 27015; 


// Initialize Winsock 
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); 
if (iResult != 0) { 
    printf("WSAStartup failed: %d\n", iResult); 
    return 1; 
} 
//----------------------------------------- 
// Set up sockaddr_in structure which is passed 
// to the getnameinfo function 
saGNI.sin_family = AF_INET; 
saGNI.sin_addr.s_addr = inet_addr(argv[1]); 
saGNI.sin_port = htons(port); 

//----------------------------------------- 
// Call getnameinfo 
dwRetval = getnameinfo((struct sockaddr *) &saGNI, 
         sizeof (struct sockaddr), 
         hostname, 
         NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV); 

if (dwRetval != 0) { 
    printf("getnameinfo failed with error # %ld\n", WSAGetLastError()); 
    return 1; 
} else { 
    printf("getnameinfo returned hostname = %s\n", hostname); 
    return 0; 
} 

} Ce code me retourne le nom d'hôte comme = 255.255.255.255 pas le nom de domaine.

Répondre

0
int WSAAPI getnameinfo(
    __in const struct sockaddr FAR *sa, 
    __in socklen_t salen, 
    __out char FAR *host, 
    __in DWORD hostlen, 
    __out char FAR *serv, 
    __in DWORD servlen, 
    __in int flags 
); 

http://msdn.microsoft.com/en-us/library/ms738532(v=VS.85).aspx

Cet appel API réprouve gethostbyaddr.

+0

@ Moo-Juice: Mec, je veux un nom de domaine de IP pas hostname. –

+0

Je vous suggère fortement de lire le lien que je vous ai envoyé. 'Un pointeur vers une chaîne ANSI utilisée pour contenir le nom d'hôte. En cas de succès, le nom d'hôte est renvoyé en tant que nom de domaine complet (FQDN) '... –

+0

@ Moo-Juice: le nom d'hôte est 255.255.255.255 :( –

Questions connexes