Link to home
Start Free TrialLog in
Avatar of johnclarke
johnclarke

asked on

Obtaining a machines IP Address

I need a routine that gets the IP address of the machine that the program is currently running on.  The compiler that I am using is Microsoft Visual C Version 6.

I would be grateful for all advice offered regarding this.

Thanks in advance

John
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

struck sockaddr_in our_addr;
char buffer[255];
gethostname(buffer,sizeof(buffer));
struct hostent *host = gethostbyname(buffer);
memcpy((char*)&our_addr.sin_addr,host->h_addr,host->h_length);

then to convert to dotted decimal

char * inet_ntoa(our_addr.sin_addr);

dont free the pointer that the above function returns as its from winsocks own heap and winsock maanages that. also if you want the ip address make sure you make a pysical copy of it to another memory location as the next winsock call may overwrite the original (see the winsock help for more info on this)
ASKER CERTIFIED SOLUTION
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial