+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get ip adrress of the local machine?

  1. #1

    How to get ip adrress of the local machine?

    How to get ip adrress of the local machine?

  2. #2
    Example:

    Code:
    void GetIPAdressesOfLocalMachine(CArray<CString, CString> &strIPAddresses)
    {
    	WSAData wsaData ;
        WSAStartup(MAKEWORD(2, 2), &wsaData) ;
    
    	char szHostName[MAX_PATH] ;
    	int nResult = gethostname(szHostName, MAX_PATH) ;
    	if (nResult != SOCKET_ERROR)
    	{
    		struct hostent *pHostent = gethostbyname(szHostName) ;
    
    		for (int nIndex = 0; pHostent->h_addr_list[nIndex] != 0; ++nIndex)
    		{
    			struct in_addr address ;
    			memcpy(&address, pHostent->h_addr_list[nIndex], sizeof(in_addr)) ;
    			char *pIPAddress = inet_ntoa(address) ;
    			strIPAddresses.Add(pIPAddress) ;
    		}
    	}
    
            WSACleanup() ;
    }
    How to use it?

    Code:
    CArray<CString, CString> strIPAddresses ;
    GetIPAdressesOfLocalMachine(strIPAddresses) ;
    Note:
    a. include <winsock.h> hreader file
    b. add ws2_32.lib in library list.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts