How to make domain name convert to IP in Dev C++?

Thank you for your help.
closed account (z05DSL3A)
Have a look at the Winsock Functions on MSDN1 there may be somthing there that dose what you want, possibliy gethostbyname().

HTH

[1] http://msdn.microsoft.com/en-us/library/ms741394(VS.85).aspx
But this site is for Visual C++.
I 'm using Dev C++.
I ran the follows which "0" is returned.
cout << gethostbyname("www.yahoo.com") << endl;
closed account (z05DSL3A)
The site is not specific to Visual C++, but it is specific to windows. The section that I pointed to is Windows API code so should be fine with Dev C++. Here is a simple program to retrieve host info about www.yahoo.com (including an IP address).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

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

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;

    DWORD dwError;

    struct hostent *remoteHost;
    char *host_name;
    struct in_addr addr;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) 
    {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    host_name = "www.yahoo.com"; 


    printf("Calling gethostbyname with %s\n", host_name);
    remoteHost = gethostbyname(host_name);
    

    if (remoteHost == NULL) 
    {
        dwError = WSAGetLastError();
        if (dwError != 0) {
            if (dwError == WSAHOST_NOT_FOUND) {
                printf("Host not found\n");
                return 1;
            } else if (dwError == WSANO_DATA) {
                printf("No data record found\n");
                return 1;
            } else {
                printf("Function failed with error: %ld\n", dwError);
                return 1;
            }
        }
    } 
    else 
    {
        printf("Function returned:\n");
        printf("\tOfficial name: %s\n", remoteHost->h_name);
        printf("\tAlternate names: %s\n", remoteHost->h_aliases);
        printf("\tAddress type: ");
        switch (remoteHost->h_addrtype) {
        case AF_INET:
            printf("AF_INET\n");
            break;
        case AF_INET6:
            printf("AF_INET\n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS\n");
            break;
        default:
            printf(" %d\n", remoteHost->h_addrtype);
            break;
        }
        printf("\tAddress length: %d\n", remoteHost->h_length);
        addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];
        printf("\tFirst IP Address: %s\n", inet_ntoa(addr));
    }

    return 0;
}


NB: You will have to link to Ws2_32.lib to build this. I don't use Dev C++, so can't help you with that.
Last edited on
Thank you for your help.
Last edited on
Topic archived. No new replies allowed.