Hi
Could someone show me a simple method for getting an IP address (working under dev-cpp + WinXp and WinServ2003) ? I need only a default address (one showed with ipconfig command). What do I have to include ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The trick is to use gethostname() to get the local host name, then use that with gethostbyname() to get hostent structure which enumerates all network adapters present. The default one being at host_entry.h_addr_list[0].
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
From the second link in the Google search an example of exactly what I said:
// Init WinSock
WSADATA wsa_Data;
int wsa_ReturnCode = WSAStartup(0x101,&wsa_Data);
// Get the local hostname
char szHostName[255];
gethostname(szHostName, 255);
struct hostent host_entry;
host_entry=gethostbyname(szHostName);
char * szLocalIP;
szLocalIP = inet_ntoa ((struct in_addr )host_entry->h_addr_list);
WSACleanup();
Note that you might consider using Stackoverflow for non Dev-C++ specific problems in the future. It is rapidly becoming the place for programming discussion, and is far more innovative and dare I say it "Web 2.0" than most forums (and especially Sourceforge which is in the dark ages by comparison!).
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
Could someone show me a simple method for getting an IP address (working under dev-cpp + WinXp and WinServ2003) ? I need only a default address (one showed with ipconfig command). What do I have to include ?
I Googled "win32 get local ip address" which yielded a number of suitable answers (including the top entry): http://windows-programming.suite101.com/article.cfm/socket_programming_gethostbyname
The trick is to use gethostname() to get the local host name, then use that with gethostbyname() to get hostent structure which enumerates all network adapters present. The default one being at host_entry.h_addr_list[0].
Clifford
From the second link in the Google search an example of exactly what I said:
// Init WinSock
WSADATA wsa_Data;
int wsa_ReturnCode = WSAStartup(0x101,&wsa_Data);
// Get the local hostname
char szHostName[255];
gethostname(szHostName, 255);
struct hostent host_entry;
host_entry=gethostbyname(szHostName);
char * szLocalIP;
szLocalIP = inet_ntoa ((struct in_addr )host_entry->h_addr_list);
WSACleanup();
Note that you might consider using Stackoverflow for non Dev-C++ specific problems in the future. It is rapidly becoming the place for programming discussion, and is far more innovative and dare I say it "Web 2.0" than most forums (and especially Sourceforge which is in the dark ages by comparison!).
Clifford
Thnx, it works