IP address of host...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    IP address of host...

    Hi.. are there any exposed functions in c which gives IP address of host directly, like in java or c#???

    If yen in which header file?

    Regards
    Dheeraj Joshi
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    It's os- and framework- dependent. There are no such functions in the standard.

    Comment

    • unauthorized
      New Member
      • May 2009
      • 81

      #3
      There is a pretty standard way to do it through. getaddrinfo() in the berkley sockets API can get you the IP. Not ANSI C, but it works on Windows, 'nix, Mac and I bet a lot more systems.

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        I got this program

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <netdb.h>
        #include <netinet/in.h>
        #include <sys/socket.h>
        #ifndef   NI_MAXHOST
        #define   NI_MAXHOST 1025
        #endif
        
        int main(void)
        {
            struct addrinfo * result;
            struct addrinfo * res;
            int error;
        
            error = getaddrinfo("www.google.com", NULL, NULL, &result);
            if (0 != error)
            {   
                fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
                return 1;
            }   
        
            for (res = result; res != NULL; res = res->ai_next)
            {   
                char hostname[NI_MAXHOST] = "";
                printf("%u\n",res->ai_addr);
                error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname,            NI_MAXHOST, NULL, 0, 0); 
                if (error != 0)
                {
                    fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
                    continue;
                }
                if (*hostname)
                {
                    printf("hostname: %s\n", hostname);
                }
             }   
            freeaddrinfo(result);
            return 0;
        }
        But unable to print the address in 10.50..... format..

        And i am unable to get local host address also... Any inputs...

        Regards
        Dheeraj Joshi

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          a. There is no point posting code and telling us "it didn't work". How didn't it work? Did it compile without errors and warnings? If it compiled did you get unexpected results when you ran it? If you got unexpected results what was the expect result? What was the unexpected result? What, if any, was the input data to the program? In the light of these questions "it didn't work" can be seen as a wholly inadequate description of your error.

          b. If you took the time to understand the program you have run instead of just running it and complaining when it doesn't produce the data you require you would get futher. In particular I suggest you examine the structures being used by this program to see if they contain the data you are interested in. If they do then you can modify the program to print out those values instead.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            That's what people often do: they search the internet for pieces of code; they copy and paste it compile it and try to run it. Most of the time it doesn't even compile and if it does it doesn't run. Surprised they enter the forums and desperately ask around: "it doesn't run, help me, urgent, plz". If they finally are spoonfed with working code they end up with another piece of not understood code but, hey, it runs. They copy it further to the internet as answers to other people's questions.

            ICT isn't a science anymore, it's like stamps collecting but now they collect code that they don't understand, but hey, it runs. And then they end up at the other end of the outsourcing pipe line and we get all that crap back. It's a shame.

            kind regards,

            Jos

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Thanks for the inputs :)

              Regards
              Dheeraj Joshi

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dheerajjoshim
                Thanks for the inputs :)

                Regards
                Dheeraj Joshi
                And now what? Are you going to try out other forums hoping they will spoonfeed you without criticism? Do you understand that snippet of code you posted or are you just hoping for the best? It won't automagically do what you have in mind.

                kind regards,

                Jos

                Comment

                Working...