Products | Support | Send comments on this topic. | Email a link to this topic. | Back to Getting Started | Help Version 18.0.10.24
LEADTOOLS Multimedia API Help

How to Get Your Own IP Address Programmatically

Show in webframe

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.

 

Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.