Thread: How can I make my program read the IP of the computer it's on?

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question How can I make my program read the IP of the computer it's on?

    How can I make my program read the IP of the computer it's on?

    Thanks, August.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    You can't do it FROM the computer, technically. But, you can make it download the page from www.whatismyip.org and then read the file.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    i think its possible to get it right off the pc itself. however the code for it is OS specific.

    i don't know much about the windows api but i've seen the code before. you would include a windows library, i think its winsock.h or something like that.

  4. #4
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    or, platform specific, use the system to call ipconfig to get the ip of active ethernet cards on windows

    use ifconfig system call on *x systems ( macosx, linux, irix, openbsd, freebsd, netbsd, unix, bsd , aix, hpux )
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by Cool-August
    How can I make my program read the IP of the computer it's on?

    Thanks, August.
    It depends which IP you want, and whether you're on a router or not. If you want your computer's local IP (e.g. The IP your router gave you) then that's one method, but if you want your internet IP (the one the rest of the world knows your modem by) that's a different method from the previous.

    Explain which one you need/would like, and it'll be easier to guide you in the correct direction.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Perhaps gethostname(..)

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Internet IP is what I want.


    char* cars;
    gethostname(cars,30);
    MessageBox(0,cars,0,0);
    // Is not working right!

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    You can't get the computer's internet IP from your computer, I'm telling you.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Why not?

    (Not that I don't belive you.)

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Will this help?

    Code:
    #include <winsock.h>
    #include <stdio.h>
    
    int main(void)
    {
        WSAData wsaData;
        struct in_addr addr;
        char *IPaddress;
        char hn[80] = {0};
        struct hostent *phostentry; 
        WSAStartup(MAKEWORD(1, 1), &wsaData);
        if (gethostname(hn, sizeof(hn)) == SOCKET_ERROR)
        {
            printf("HostName error\n");
            return 1;
        }
        printf("Hostname is %s\n", hn );
        phostentry = gethostbyname(hn);
        if (phostentry == 0) {
            printf("Error on host lookup\n");
            return 1;
        }
        for (int i = 0; phostentry->h_addr_list[i] != 0; ++i)
        {
            memcpy(&addr, phostentry->h_addr_list[i], sizeof(struct in_addr));
            IPaddress = inet_ntoa(addr);
            printf("address %d: %s\n",i,IPaddress);
        }
        WSACleanup();   
        return 0;
    }
    Bob

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    I dont know why, but you don't see it in ipconfig and that's the whole reasons sites like www.whatismyip.com exist. It's not like they'd hide it on the ipconfig menu.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But I guess it's possible, because IE and other browsers send the ip to that server first...

  13. #13
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    Hey people everyhting is possible!
    And you can even get your ip form ur own pc!
    To get your IP just enter the dos prompt or command prompt and type: ipconfig
    You will get all that you want!

    Regards!

  14. #14
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    well if u do ipconfig and your on a router ure gonna get the ip the router gives u.. at least thats wat i get and im assuming thats the way it is. even with netstat im not getting the matching ip that www.whatismyip.com gives me . hmm
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    Your computer doesn't send the info to the websites, the website finds it out itself.

    Just download www.whatismyip.org in the background to a file using some function, and then read it in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  3. Read from file and make few things
    By xxxrugby in forum C Programming
    Replies: 12
    Last Post: 07-15-2006, 10:13 AM
  4. How to make the program not to wait for the function?
    By maxorator in forum C++ Programming
    Replies: 10
    Last Post: 10-02-2005, 03:02 PM
  5. how to make certain file types open IN your program
    By DarkViper in forum Windows Programming
    Replies: 4
    Last Post: 02-06-2003, 11:37 PM