How to Get Your Own IP Address Programmatically

The following function shows how to obtain your machine's host name (IP address or domain name) programmatically, using the Windows Sockets APIs, and use it for initializing the server side.

CString GetDefaultConnection() 
{ 
   CString s; 
   WSADATA wsaData; 
   int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 
   if (iResult == NO_ERROR) 
   { 
      char Hostname[100]; 
      if(gethostname( Hostname, sizeof( Hostname )) != SOCKET_ERROR) 
      { 
         HOSTENT* pHostEnt = gethostbyname( Hostname ); 
         if(pHostEnt) 
         { 
            if(pHostEnt->h_addr_list[0]) 
            { 
               s.Format("%d.%d.%d.%d", 
               (int) (BYTE) pHostEnt->h_addr_list[0][0], 
               (int) (BYTE) pHostEnt->h_addr_list[0][1], 
               (int) (BYTE) pHostEnt->h_addr_list[0][2], 
               (int) (BYTE) pHostEnt->h_addr_list[0][3]); 
            } 
         } 
      } 
      WSACleanup(); 
   } 
   return s; 
} 

If there are multiple names (IP addresses), you can force any one you want.

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Multimedia C API Help