0 Members and 1 Guest are viewing this topic.
#include <iostream>#include <winsock.h>int main(int argc, char** argv){ WSAData wsaData; if( WSAStartup(MAKEWORD(1, 1), &wsaData) != 0 ) return 1; char ac[80]; if(gethostname(ac, sizeof(ac)) == SOCKET_ERROR) { cerr << "Error " << WSAGetLastError() << " when getting local host name." << endl; return 1; } cout << "Host name is " << ac << "." << endl; struct hostent *phe = gethostbyname(ac); if(phe == 0) { cerr << "Bad host lookup." << endl; return 1; } for(int i = 0; phe->h_addr_list[i] != 0; ++i) { struct in_addr addr; memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); cout << "Address " << i << ": " << inet_ntoa(addr) << endl; } WSACleanup(); getchar(); return 0;}
The method listed on the page above doesn't appear to work for DHCP assigned addresses. Ultimately the gethostbyname command scans the /etc/hosts file for the hostname provided. I've found that in our lab we have an entry such as 127.0.0.2, while ifconfig shows an address of 10.194.157.197
I don't know what WSA is
It gives multiple addresses, though