network program

This is a discussion on network program within the Networking/Device Communication forums, part of the General Programming Boards category; I got the following code off of Beej's Network programming guide: Code: /* ** showip.c -- show IP addresses for ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    network program

    I got the following code off of Beej's Network programming guide:

    Code:
    /*
    ** showip.c -- show IP addresses for a host given on the command line
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    
    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");
            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
    
        return 0;
    }
    I am using cygwin to compile this and I am getting the following error messages:

    8:24: sys/socket.h: No such file or directory
    9:19: netdb.h

    (does this for all of em except for stdio and string)

    In function main:
    storage size of hints isn't known
    INET6_ADDRSTRLEN undeclared
    AF_UNSPEC undeclared

    (does this for all the network variables)

    41:dereferencing pointer to an incomplete type
    42:dereferencing pointer to an incomplete type
    45:dereferencing pointer to an incomplete type
    46:dereferencing pointer to an incomplete type
    51:dereferencing pointer to an incomplete type

    Can someone please help me?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Perhaps that is not for windows.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    cygwin is for windows and so is the program.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Poincare View Post
    cygwin is for windows and so is the program.
    If you say so. I don't use windows, so I was just guessing (this program will definately run on *nix, by the way), partially because I don't think you could ever have a path containing this:
    Code:
    sys/socket.h
    which these are all header files in /usr/include, but if you do not have a /usr/include, and your path notation uses a backslash and not a forward slash, I would guess this is actually NOT for windows.

    But again, just a guess -- since I don't actually use windows, there is a lot I don't know about it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As Beej says, these aren't native Windows things (the native Windows header is winsock.h instead). Depending on how you installed cygwin, you may or may not have this -- the easy way is (inside cygwin, where I'm assuming you are typing gcc or the like) try "cd /usr/include/sys" and see what you get. If you find things there, then we can look at other settings.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    I have that directory...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't have cygwin on this machine, so I can't go much farther, but I'm assuming you're typing "gcc" from inside cygwin. "gcc -print-search-dirs" should give you a list of where it's looking for libs. If /usr/include isn't on the list, then you'll have to specify it on the command line (with -L).

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Quote Originally Posted by MK27
    I would guess this is actually NOT for windows.
    This is for Windows. There is nothing wrong with using forward slashes to denote include paths.

    Quote Originally Posted by Poincare
    I have that directory...
    Is the file "socket.h" in the directory?

    Quote Originally Posted by tabstop
    If /usr/include isn't on the list, then you'll have to specify it on the command line (with -L).
    Isn't it -I, not -L? (I'm assuming that gcc on cygwin is the same as on Linux)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. How to start program P2P network?
    By gogo in forum C Programming
    Replies: 2
    Last Post: 07-27-2004, 08:35 AM
  4. network program
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 11:47 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21