Get the machine IP address

All questions regarding Windows programming, post here. API,COM, ActiveX, DirectX, OpenGL, MFC and so on...

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Get the machine IP address

Postby Rodrigo Dias » Sat May 08, 2004 11:13 pm

How do I get the IP address of the machine in which the program is running?
"I don't pay women for sex,
I pay them to go away." - Jack Nicholson
Rodrigo Dias
 
Posts: 849
Joined: Thu Apr 22, 2004 6:51 am
Location: Natal, Brazil

hmm

Postby disckcr33t » Sun May 09, 2004 6:44 pm

i remember answering a question exactly the same as this a few days ago , in the same exact forum..

you could type 'ipconfig' and it will tell you, but if you mean in a program then im not sure, possable have it copy it from a website like www.ipchicken.com or www.whatsmyip.org but i dont know how to do that
disckcr33t
 

Postby Rodrigo Dias » Sun May 09, 2004 8:52 pm

Yes, I mean inside a program, by calling some function or group of functions. Maybe there is a way with Winsock API, but I couldn't find one.
"I don't pay women for sex,
I pay them to go away." - Jack Nicholson
Rodrigo Dias
 
Posts: 849
Joined: Thu Apr 22, 2004 6:51 am
Location: Natal, Brazil

Postby Multiplexor » Mon May 10, 2004 6:40 am

Its easy to miss how to do this when going through the sockets library, as (in my opinion) the sockets specification is very poorly designed and unintuitive. Anyway, now that my rant is over, the functions you need to use are gethostname, gethostbyname, and probably inet_ntoa.

int gethostname(char*, size_t) gets the 'host name' for the local machine (you pass it a poitner to a char* string that you have allocated, and the maximum length of the string). Essentially, it gets the DNS name that will resolve to the machine's name.

struct hostent* gethostbyname(char*) takes the hostname that you got from gethostname and resolves it to the addresses on the local machine. It returns a structure that looks like the following:

Code: Select all
struct hostent {
        char    *h_name;        /* official name of host */
        char    **h_aliases;    /* alias list */
        int     h_addrtype;     /* host address type */
        int     h_length;       /* length of address */
        char    **h_addr_list;  /* list of addresses */
}


The h_addr_list field is the one you're interested in. It returns an array of IP addresses that are associated with the host. These IP addresses, however, are in network byte order -- useful to sockets, useless to humans. To convert these addresses to a readable IP address string, use the inet_ntoa function.

char* inet_ntoa(struct in_addr) takes and in_addr struct specifying the address in network byte order and returns a string that is a regular, readable IP address (www.xxx.yyy.zzz). The in_addr structure and the bytes in h_addr_list are stored in the same format, so you can use memcpy to copy an address from h_addr_list into a struct in_addr.

For an example that puts this all together in some code, see get Local IP addresses example.
Multiplexor
 
Posts: 43
Joined: Sun Apr 25, 2004 5:44 pm


Return to Windows Programming

Who is online

Users browsing this forum: No registered users and 1 guest