Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Canton, OH
    Posts
    2
    Rep Power
    0

    ip address of machine?


    I need to somehow discover the IP address of the machine running the c++ program. I've searched the web and only 1 result was even have worth my time, but it doesn't work all the time (it does some hostname lookup stuff -- i just want the ip - even if a hostname doesn't exist)

    I talked to someone and they said there is some way to talk to the operating system (in this case windows) and get this information. anyone have any ideas?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    even if a hostname doesn't exist
    afaik this is not possible / valid.

    a machine can have as many ips as it wants (ok, limited to about 2^32 minus some). no limit i know of for host names.
    so you need to check all of them and sort out all local hosts (127.*.*.*)

    also, afaik there is no standardized function for reading the IPs, so your program will not be portable and for more specific infos you need to tell us your target OS and compiler.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Canton, OH
    Posts
    2
    Rep Power
    0
    ok... its going to be running under windows xp/2000 and be compiled with visual studio 6 (in c++)

    and i tried the ones i found online, and had compile problems (it was complaining about gethostbyname() function)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    compile problems? you did not include the header file winsock2.h, right?

    i guess you want to read this:
    http://msdn.microsoft.com/library/de...eference_2.asp
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0
    It's possible to use dos command from a C++ program using system ()

    So your code would look something like these:

    #include <process.h>

    void main( void )
    {
    system( "ipconfig" );
    system("pause");
    }

    The code above would produce a dos screen containing
    information about ip adress, subnetmask , default gateway.

    hope these help..

    ~skumle
  10. #6
  11. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    At first, invoking other programs with system() function is not recommended.

    You can do it with winsock. Here's the code - it's dwise's and in C. I just changed it a little.
    Code:
    //set up server & ither things before using this
    if(gethostname(handler, 40)){
    			cgIP(WSAGetLastError());
    			printf("Your computer name: %s\n",handler);
    		}
    
    		he = gethostbyname(handler);
    
    		if(he == NULL){
    			cgIP(WSAGetLastError());
    		}
    
    		if(sets.IPno == -1){
    			printf("IP Addresses:\n");
    			for (int i = 0; he->h_addr_list[i] != 0; ++i){
    				memcpy(&addr, he->h_addr_list[i], sizeof(struct in_addr));
    				printf("\tAddress #%d: %s\n",i,inet_ntoa(addr));
    				
    			}
    		}
    cgIP looks like this:
    Code:
    void cgIP(int WSAerror){
         printf("Can't get your IP. Error code: %d\n", WSAerror)
    }
    Last edited by Loser; September 9th, 2003 at 08:31 AM.
  12. #7
  13. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    there are various ioctl() calls to enumerate the interfaces and their addresses; however i have no idea if windows even has an ioctl() function. if someone else can confirm this i'll post the code to do so.
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    just remembered an easier way to do it. now if your on a multi-homed host this will only get the outgoing interface to the net, but im pretty sure you're not that case, so...

    you can create a SOCK_DGRAM socket(UDP), and then call connect on it. the connect socket address structure should contain the address of a well known host, like aol.com or microsoft.com, etc... you call connect, this sends nothing out, it just determines what interface will be used, then u call getsockname() on the socket and get ur ip. little hackish but easier than the ioctl() calls and platform independent.(well as long as M$ implements tcp/ip correctly...*crosses fingers*)
  16. #9
  17. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    So you need no connecting. If that's true, it could work. :)

    I tried to connect to myself, but if you enter 127.0.0.1, you receive a dgram from 127.0.0.1.
  18. #10
  19. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    not sure what you mean, how are you recieving a datagram?
    here is my code
    Code:
    #include "my_sock.h"
    
    main()
    {
            int sock = socket(AF_INET, SOCK_DGRAM, 0),      len = 16;
            struct sockaddr_in sa;  bzero(&sa, 16);
            char    addr[50];
            
            sa.sin_family = AF_INET,        sa.sin_port = htons(80),        sa.sin_addr.s_addr = inet_addr("205.188.145.214");//aol.com
    
    //      sa.sin_addr.s_addr = inet_addr("127.0.0.1"); //when i did this it worked, gave me localhost as addy
    
            connect(sock,(struct sockaddr *) &sa, len);
    
            struct sockaddr_in addy;        bzero(&addy, len);
    
            getsockname(sock, (struct sockaddr *)&addy, &len);
    
            printf("Address is %s\n", inet_ntop(AF_INET, &addy.sin_addr.s_addr,
                                            addr, 50)       );
    
            
    
    }
    works fine for me. :)
  20. #11
  21. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    Infamous, what does inet_ntop() do? I'm using lcc-Win32 and there's no such function...

    I used inet_ntoa() and it wrote 127.0.0.1...

    I need my outside address, like 169.12.0.23...
    Last edited by Loser; September 21st, 2003 at 10:59 AM.
  22. #12
  23. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    from inet_ntop man pages
    inet_ntop(3) extends the inet_ntoa(3) function to support multiple
    address families, inet_ntoa(3) is now considered to be deprecated in
    favor of inet_ntop(3).
    are you using teh aol address for connect? or the local host address? it looks like you are using the line i commented out? after reading my m$ windows netprog book, i see that inet_ntop isnt even a valid library function in windows. inet_ntoa is still deprecated, the function they want you to use is getaddrinfo(). kind of a pain for just doing something simple, but.... im going to test this on my windows machine and see how it works. bbk soon
    Last edited by infamous41md; September 21st, 2003 at 03:48 PM.
  24. #13
  25. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    sorry it was my fault it wasnt working. im not used to windows, so i forgot to load the WSA crap up. I'm typing this from my windows box which i successfully ran this code:

    Code:
    //#define PENGUIN
    
    #include <stdio.h>
    
    #ifndef PENGUIN
    
    #include <winsock2.h>
    
    #else
    #include "my_sock.h"
    
    #endif
    
    int
    main()
    {
    #ifndef PENGUIN
            WSADATA       wsaData;
            WSAStartup(MAKEWORD(1, 1), &wsaData);
    #endif
            int      len = 16, sock = 0;
            struct sockaddr_in sa, addy;  
            memset(&addy, 0, len),  memset(&sa, 0, 16);
    
            sa.sin_family = AF_INET;       
            sa.sin_port = htons(80);
            sa.sin_addr.s_addr = inet_addr("205.188.145.214");//aol.com
    
            sock = socket(AF_INET, SOCK_DGRAM, 0);
      
            connect(sock, (struct sockaddr *) &sa, len);
    
            getsockname(sock, (struct sockaddr *)&addy, &len);
    
            printf("Address is %s\n", inet_ntoa(addy.sin_addr) );
    
            return 0;        
    }
    edit: i'd also like to add that this would not be the preferred way of doing this in a real application. the call to connect binds the socket to an address by examing the routing tables and deciding which interface to send the packet on. so if a host had multiple interfaces, the address returned will depend on the direction of the packet. i.e. on a gateway box if one address is for the INTERNET and another for the LAN, the address returned depends where its going outer/inner. the correct way would be to use ioctl() to enumerate the interfaces, examples of which are found here:
    http://www.kohala.com/start/unpv12e.html
    Last edited by infamous41md; September 21st, 2003 at 04:35 PM.
  26. #14
  27. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    I ran the code and it always returns 0.0.0.0...
    For windows, there's no ioctl(), just ioctlsocket()... But It doesn't seem useful... :(
  28. #15
  29. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    Look, look, what have I found... :)
    Code:
    #  define  UC(b)  (((int)b)&0xff)
    
    
    // get ip address using gethostname and gethostbyname
    int GetIpAddress( char *szIpAddress )
    {
    	 struct hostent *hp;
    
    	 char szHost[256];  // should be big enough.
    
    	 if( !szIpAddress || ! *szIpAddress )
    		  return FALSE;
    
    	 memset(szHost, 0, 256);
    
    	 gethostname(szHost, 256);
    	 hp = gethostbyname(szHost);
    
    	 if( ! hp )
    		 return FALSE;
    
    	 // hp->h_name == full computer name (internet name)
    
    	 printf("%d.%d.%d.%d",
    				UC(hp->h_addr[0]), UC(hp->h_addr[1]),
    				UC(hp->h_addr[2]), UC(hp->h_addr[3]));
    
    	 return TRUE;
    }
    Don't worry, I didn't write this. I just google'd :)
    It works on win98 FE.
    Last edited by Loser; September 22nd, 2003 at 11:35 AM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo