Isavel
Isavel

Reputation: 45

about getadrrinfo() C++?

I'm reading this book called beej's guide to network programming and there's a part in the book were it provide a sample code which illustrate the use of getaddrinfo(); the book state that the code below "will print the IP addresses for whatever host you specify on the command line" - beej's guide to network programming.

now I'm curious and want to try it out and run the code, but I guess the code was develop in UNIX environment and I'm using visual studio 2012 windows 7 OS, and most of the headers was not supported so I did a bit of research and find out that I need to include the winsock.h and ws2_32.lib for windows, for it to get working, fortunately everything compiled no errors, but when I run it using the debugger and put in 'www.google.com' as command argument I was disappointed that it did not print any ipaddress, the output that I got from the console is "getaddrinfo: E"

Any help would be appreciated.

#ifdef _WIN32
#endif

#include <sys/types.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>

using namespace std;

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <winsock.h>

#pragma comment(lib,  "ws2_32.lib")

int main(int argc, char *argv[])
{
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];
    if (argc != 2) {
        fprintf(stderr,"usage: showip hostname\n");
        system("PAUSE");
        return 1;
    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;
    if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
        system("PAUSE");
        return 2;
    }
    printf("IP addresses for %s:\n\n", argv[1]);
    for(p = res;p != NULL; p = p->ai_next) {
        void *addr;
        char *ipver;
        // get the pointer to the address itself,
        // different fields in IPv4 and IPv6:
        if (p->ai_family == AF_INET) { // IPv4
            struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4";
        } else { // IPv6
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6";
        }
        // convert the IP to a string and print it:
        inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
        printf(" %s: %s\n", ipver, ipstr);
    }
    freeaddrinfo(res); // free the linked list

    system("PAUSE");

    return 0;
}

Upvotes: 1

Views: 501

Answers (2)

WhozCraig
WhozCraig

Reputation: 66194

You neglected to initialize the Windows socket layer, a requirement when using the socket API under Windows. Use the WSAStartup() function to do this.

int main(int argc, char *argv[]) 
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return 1;
    }

   ... your code here...

    WSACleanup();
}

Upvotes: 1

Sebastian
Sebastian

Reputation: 1889

Maybe you get "getaddrinfo: E" because you somehow got a 16 bit unicode version of gai_strerror and try to use it in an 8 bit printf. So the character 'E' consists of an 'E' byte and a zero byte, which terminates the string when interpreted as 8 bit string.

Not sure about that. I just got the idea when I saw here http://msdn.microsoft.com/en-us/library/windows/desktop/ms738514(v=vs.85).aspx that there's also an ANSI version.

Upvotes: 0

Related Questions