| View previous topic :: View next topic |
| Author |
Message |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Sat Mar 28, 2009 4:14 pm Post subject: winsock struct addrinfo |
|
|
| How do I retrieve my IP address with the addrinfo struct?
|
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Sat Mar 28, 2009 5:10 pm Post subject: |
|
|
http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
...
GetAddrInfo() sample is exactly what u need....
regards BanMe
_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you. |
|
| Back to top |
|
 |
LolSalad Grandmaster Cheater
Reputation: 1
Joined: 26 Aug 2007 Posts: 988 Location: Australia
|
Posted: Sat Mar 28, 2009 5:18 pm Post subject: |
|
|
I'll assume you want your LAN IP(s), not external. Here's how I did it in the past, it retrieves all of your LAN IPs into ipArray.
| Code: | char hostName[256];
if (gethostname(hostName, sizeof(hostName)) != 0) {
// Error
}
hostent* rHost = NULL;
rHost = gethostbyname(hostName);
if (!rHost) {
// Error
}
unsigned int ipCount = sizeof(rHost->h_addr_list) / rHost->h_length;
char** ipArray = NULL;
ipArray = new char*[ipCount];
in_addr rInAddr;
for (unsigned int i = 0; i < ipCount; i++) {
ipArray[i] = new char[15];
memcpy(&rInAddr, rHost->h_addr_list[i], rHost->h_length);
strcpy_s(ipArray[i], 15, inet_ntoa(rInAddr));
printf("IP retrieved: %s\n", ipArray[i]);
} |
Once used, you should delete ipArray.
| Code: | delete [] *ipArray;
delete [] ipArray; |
_________________
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Sat Mar 28, 2009 5:22 pm Post subject: |
|
|
| Thank you LolSalad. I will now give you rep.
|
|
| Back to top |
|
 |
LolSalad Grandmaster Cheater
Reputation: 1
Joined: 26 Aug 2007 Posts: 988 Location: Australia
|
Posted: Sat Mar 28, 2009 5:51 pm Post subject: |
|
|
After messing around with it a bit I noticed that ipCount is incorrect (always 1), and I don't know of any way to get the correct number.
Edit: This seems to work fine. | Code: | char hostName[256];
if (gethostname(hostName, sizeof(hostName)) != 0) {
error("gethostname failed");
return;
}
hostent* rHost = NULL;
rHost = gethostbyname(hostName);
if (!rHost) {
error("gethostbyname failed");
return;
}
unsigned int ipCount = 0;
char** ipArray = NULL;
in_addr rInAddr;
for (unsigned int i = 0; rHost->h_addr_list[i] != 0; i++) {
ipCount++;
char** nIPArray = new char*[ipCount];
for (unsigned int j = 0; j < i; j++) nIPArray[j] = ipArray[j];
delete [] ipArray;
ipArray = nIPArray;
ipArray[i] = new char[15];
memcpy(&rInAddr, rHost->h_addr_list[i], rHost->h_length);
strcpy_s(ipArray[i], 15, inet_ntoa(rInAddr));
printf("IP retrieved: %s\n", i + 1, ipArray[i]);
} |
Also, the delete should probably be | Code: | for (unsigned int i = 0; i < ipCount; i++) delete [] ipArray[i];
delete [] ipArray; |
_________________
|
|
| Back to top |
|
 |
|