Detecting Local IP Address
-
Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.
-
Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.
Assuming that you have only one network interface (your eth card) and a loopback interface (127.0.0.1), i think you could try to get the name of the local computer, and then resolve it in an ip address .
char szMyComputerName[0xFF] = {0}, szMyIpAddress[0xFF] = {0}; DWORD dwSize = 0xFF; ::GetComputerName(szMyComputerName,&dwSize);
Now all you have to to is to resolve 'szMyComputerName' as you did in your example . Naturally, if you have more than one network interface (eth card, wireless, etc), you have to enumerate them and then you choose the one you want to work on :SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); INTERFACE_INFO InterfaceList[20]; unsigned long nBytesReturned; WSAIoctl( sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0); for( int i = 0; i < nBytesReturned / sizeof(INTERFACE_INFO); i++ ) { printf( "Interface[%d] : %s\n",i, inet_ntoa( ((sockaddr_in *)&(InterfaceList[i].iiAddress))->sin_addr ) ) }
Hope i helped ....:) -
Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.
wasife wrote:
ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr);
replace ur code above with line below: char* localIP=inet_ntoa(*(LPIN_ADDR)*(localhost->h_addr_list)); It should be work, but I can't tell why ur code doesn't. wish this could help!
-
Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.
Try this (works on my machines)
char* hostName=new char[255];
if (0 == ::gethostname(hostName, 255))
{
HOSTENT *pHostent = ::gethostbyname(szTempName);
if (pHostent && pHostent->h_addrtype == AF_INET)
{
in_addr inaddr = *(in_addr *)(pHostent->h_addr_list[0]);
char *pAddStr = ::inet_ntoa(inaddr);
if (pAddStr)
{
AfxMessageBox(pAddStr);
}
}
}