get local ip 
Author Message
 get local ip

I want to get the local ip of my box (running win2k) and
for a reason that I can't understand my following code
always returns 0. Any ideas of what causes the problem and
I can't get the ip?

Elias Bakatsis

DWORD GetHostID()
{
   char szLclHost[120];
   LPHOSTENT lpstHostent;
   SOCKADDR_IN stLclAddr;
   SOCKADDR_IN stRmtAddr;
   int nAddrSize = sizeof(SOCKADDR);
   SOCKET hSock;
   int nRet;

   /* Init local address (to zero) */
   stLclAddr.sin_addr.s_addr = INADDR_ANY;

   /* Get the local hostname */
   nRet = gethostname(szLclHost, sizeof(szLclHost));
   if (nRet != SOCKET_ERROR)
      {
      /* Resolve hostname for local address */
      lpstHostent = gethostbyname((LPSTR)szLclHost);
      if (lpstHostent)
         stLclAddr.sin_addr.s_addr = *((u_long FAR*)
(lpstHostent->h_addr));
      }

   /* If still not resolved, then try second strategy */
   if (stLclAddr.sin_addr.s_addr == INADDR_ANY)
      {
      /* Get a UDP socket */
      hSock = socket(AF_INET, SOCK_DGRAM, 0);
      if (hSock != INVALID_SOCKET)
         {
         /* Connect to arbitrary port and address (NOT
loopback) */
         stRmtAddr.sin_family = AF_INET;
         stRmtAddr.sin_port   = htons(IPPORT_ECHO);
         stRmtAddr.sin_addr.s_addr = inet_addr
("128.127.50.1");
         nRet = connect(hSock, (LPSOCKADDR)&stRmtAddr,
sizeof(SOCKADDR));
         if (nRet != SOCKET_ERROR)
            {
            /* Get local address */
            getsockname(hSock, (LPSOCKADDR)&stLclAddr, (int
FAR*)&nAddrSize);
            }
         closesocket(hSock);   /* we're done with the
socket */
         }
      }

   return stLclAddr.sin_addr.s_addr;

Quote:
}



Mon, 04 Apr 2005 18:33:57 GMT  
 get local ip
This question is off-topic here. You may want to ask in
microsoft.public.platformsdk.networking
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:

> I want to get the local ip of my box (running win2k) and
> for a reason that I can't understand my following code
> always returns 0. Any ideas of what causes the problem and
> I can't get the ip?



Mon, 04 Apr 2005 23:35:34 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Getting Local IP address

2. getting local IP address

3. getting local ip address

4. Newbie : Getting local IP Urgent!! Please HELP!!!

5. Newbie : Getting local IP Urgent!! Please HELP!!!

6. getting ip address of the default route interface (public ip vs private ip)

7. Local IP address

8. How to get IP address (local)?

9. Question:obtaining local ip address

10. Local IP address

11. Need local IP address ...

12. retrieve IP address of local host

 

 
Powered by phpBB® Forum Software