Thread: InetNtop undeclared identifier

  1. #1
    Registered User Vana Papi's Avatar
    Join Date
    Jul 2012
    Posts
    11

    InetNtop undeclared identifier

    Hi,
    I'm trying to launch the code from Beej's "Guide to Network Programming".
    I tried to google, but I didn't find a solution. Other people were able to fix SIMILAR problems with adding some header files or a lib, but it didn't work for me.
    The code looks like this(there are unnecessary headers, I know, but they are added just to rule out the possible header issue ) :
    Code:
    #include <iostream>
    #include <winsock2.h>
    #include <windows.h>
    #include <Ws2tcpip.h>
     
     
    int main(int argc, char *argv[])
    {
        WSADATA wsaData; // if this doesn't work
        //WSAData wsaData; // then try this instead
        // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
        if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
        fprintf(stderr, "WSAStartup failed.\n");
        exit(1);
        }
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];
    if (argc != 2) {
    fprintf(stderr,"usage: showip hostname\n");
    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));
    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
    WSACleanup();
    return 0;
    }
    Now I get inet_ntop undeclared(first use this in function).
    I've tried to replace it with inetNtop, and PCTSTR WSAAPI InetNtop. I still get the same thing.

    I've added 'libws2_32.a' and 'libsock32.a' under project->project options->parameters->add library or object in Dev-C++.

    I know the InetNtop should be avoided in the first place, but I'm trying to understand network programming before I start with the alternatives.

    Any idea whats wrong?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is your aim to write C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User Vana Papi's Avatar
    Join Date
    Jul 2012
    Posts
    11
    Quote Originally Posted by Elysia View Post
    Is your aim to write C or C++?
    Beej's guide and examples use C, but I aim to write c++.

    Are there alternative functions(except printf, which I can edit out in the first post).
    Last edited by Vana Papi; 08-01-2012 at 03:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undeclared identifier and if problem
    By Ben Atkins in forum C++ Programming
    Replies: 1
    Last Post: 05-31-2012, 04:21 PM
  2. Undeclared Identifier
    By Kayoss in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2006, 10:48 AM
  3. 'setioflags' undeclared identifier?
    By ForlornOdium in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2003, 04:44 AM
  4. 'undeclared identifier' in tapi
    By schez in forum Windows Programming
    Replies: 2
    Last Post: 10-31-2003, 10:06 PM
  5. Undeclared Identifier
    By Witch_King in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2001, 12:58 PM

Tags for this Thread