CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2007
    Posts
    9

    Why WSAStartup couldn't get the localhost ip address

    Hi everyone,
    I had used the WSAStartup & gethostbyname to get a list of ipaddress from the local machine. However the ipaddress for the local host was not shown, Is there anyway retrieve all the usable ipaddress which include the local host? and why is it not shown using WSAData?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Why WSAStartup couldn't get the localhost ip address

    Can you show as the code that you used?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 2007
    Posts
    9

    Re: Why WSAStartup couldn't get the localhost ip address

    Hi my code are as below, I was wondering is there anyway to retrieve the local address too, e.g localhost 127.0.0.1

    Code:
    char ac[80];
    struct in_addr addr;      
     
    
    WSAData wsaData;
    
     
    
     //Initialize structure
     memset(&addr,0,sizeof(addr));
     memset(&wsaData,0,sizeof(wsaData));
     
     //Specify the version of Windows Sockets 
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
            return false;
        }
     
     //Getting the standard host name for the local machine
        if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
            return false;
        }
    
     //Retrieving host information corresponding to a host name
     struct hostent *phe = gethostbyname(ac);
        if (phe == FAILURE){
            return false;
        }
     
     //Retrieving list of addresses for the host
        for (int i = 0; phe->h_addr_list != 0; ++i){
            memcpy(&addr, phe->h_addr_list, sizeof(struct in_addr));
        }
    
     
    
     WSACleanup();
    
    return true;

  4. #4
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Why WSAStartup couldn't get the localhost ip address

    Hi,

    You can use WSAGetLastError to determine the exact error you are receiving.
    If there is no love sun won't shine

  5. #5
    Join Date
    Dec 2007
    Posts
    9

    Re: Why WSAStartup couldn't get the localhost ip address

    Hi miteshpandey,
    I don' t think thre is anything wrong with the code, so I dun really need WSAGetLastError to find the error, however I do want to know is there anyway that I can retrieve ip address that with the the local host address included? I had tried to use winsock to find the ip address but it doesn't include the local ip 127.0.0.1 and IP helper is having the same problems. Therefore is there a way to get all the ip which include the local host address?

  6. #6
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Why WSAStartup couldn't get the localhost ip address

    Not 100% sure but since local host is always 127.0.0.1 it may have been thought redundant to be retrieved by the function. If you are using a list to store the ip addresses, then you can just add 127.0.0.1 as hard coded entry.
    If there is no love sun won't shine

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Why WSAStartup couldn't get the localhost ip address

    I had used the WSAStartup & gethostbyname to get a list of ipaddress from the local machine.
    But you should be using GetAdaptersInfo() or GetAdaptersAddresses().
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Dec 2007
    Posts
    9

    Re: Why WSAStartup couldn't get the localhost ip address

    Hi miteshpandey,
    Are you sure that the local Ip is always 127.0.0.1? If I remember correctly the reserved IPv4 address block for loopback is 127.0.0.0/8, therefore is it really ok to hardcode the localhost address?

    Hi Cilu,
    Do you have any examples or links on the GetAdaptersInfo() or GetAdaptersAddresses() for c++,Vs6 as I only manage to find it on c#.

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Why WSAStartup couldn't get the localhost ip address

    Well, did you take a look in MSDN?

    http://msdn2.microsoft.com/en-us/library/aa365917.aspx
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Jun 2005
    Posts
    1,255

    Re: Why WSAStartup couldn't get the localhost ip address

    The list of IP addresses that you are retrieving are those that are listed in your .hosts file. There can be several other IP addresses that can be reached from your computer, and that are not listed in this file.

    The localhost IP usually is 127.0.0.1. There is a little difference with 127.0.0/8. See http://en.wikipedia.org/wiki/Localhost

    A computer can have more than one IP address. Your computer has the 127.0.0.1 address and also another one if it is in a network.

  11. #11
    Join Date
    May 2024
    Posts
    4

    Re: Why WSAStartup couldn't get the localhost ip address

    One IP 'loopback'/localhost address might be sufficient, but you might possibly want to use a different one other than the standard one of '127.0.0.1' (- at least if this is for a special/specific usage); this is where IPv6 is a downgrade (/sucks in comparison to IPv4) as it only has one loopback address. ;/

    You might be looking for something like this:


    // DVM: S: Add loopback adapter

    auto currIpAdapterInfo = m_pInfo;
    IP_ADAPTER_INFO* parentIpAdapterInfo = nullptr;

    while (currIpAdapterInfo != nullptr)
    {
    parentIpAdapterInfo =
    currIpAdapterInfo;

    currIpAdapterInfo =
    currIpAdapterInfo->Next;
    }

    // ReSharper disable once CppLocalVariableMayBeConst
    bool addLoopbackAdapter = true;

    if (addLoopbackAdapter &&
    parentIpAdapterInfo != nullptr)
    {
    IP_ADAPTER_INFO* loopbackIpAdapterInfo =
    new IP_ADAPTER_INFO;

    loopbackIpAdapterInfo->Next = nullptr;

    loopbackIpAdapterInfo->ComboIndex = 999;

    strcpy(loopbackIpAdapterInfo->AdapterName, _T("Loopback"));

    strcpy(loopbackIpAdapterInfo->Description, _T("Loopback Pseudo-Interface"));

    // Hw address length
    loopbackIpAdapterInfo->AddressLength = 6;

    // Hw address
    // 2, 0, 76, 79, 79, 80, 0, 0 - '\x2', '\0', "LOOP", '\0', '\0'
    //memset(loopbackIpAdapterInfo->Address, 0, sizeof(loopbackIpAdapterInfo->Address));

    loopbackIpAdapterInfo->Address[0] = '\x2';
    loopbackIpAdapterInfo->Address[1] = '\0';
    loopbackIpAdapterInfo->Address[2] = 127;
    loopbackIpAdapterInfo->Address[3] = 4;
    loopbackIpAdapterInfo->Address[4] = 0;
    loopbackIpAdapterInfo->Address[5] = 2;
    loopbackIpAdapterInfo->Address[6] = 0;
    loopbackIpAdapterInfo->Address[7] = 0;

    //loopbackIpAdapterInfo->Address[0] = '\x2';
    //loopbackIpAdapterInfo->Address[1] = '\0';
    //loopbackIpAdapterInfo->Address[2] = 'L';
    //loopbackIpAdapterInfo->Address[3] = 'O';
    //loopbackIpAdapterInfo->Address[4] = 'O';
    //loopbackIpAdapterInfo->Address[5] = 'P';
    //loopbackIpAdapterInfo->Address[6] = '\0';
    //loopbackIpAdapterInfo->Address[7] = '\0';

    loopbackIpAdapterInfo->Index = 21; // ???
    loopbackIpAdapterInfo->Type = 71; // ??
    loopbackIpAdapterInfo->DhcpEnabled = 0;
    loopbackIpAdapterInfo->CurrentIpAddress = nullptr;

    loopbackIpAdapterInfo->IpAddressList.Next = nullptr;
    strcpy(loopbackIpAdapterInfo->IpAddressList.IpAddress.String, _T("127.4.0.2"));
    strcpy(loopbackIpAdapterInfo->IpAddressList.IpMask.String, _T("0.0.0.0"));
    loopbackIpAdapterInfo->IpAddressList.Context = 0;

    loopbackIpAdapterInfo->GatewayList.Next = nullptr;
    strcpy(loopbackIpAdapterInfo->GatewayList.IpAddress.String, _T("0.0.0.0"));
    strcpy(loopbackIpAdapterInfo->GatewayList.IpMask.String, _T("255.255.255.255"));
    loopbackIpAdapterInfo->GatewayList.Context = 0;

    loopbackIpAdapterInfo->DhcpServer.Next = nullptr;
    loopbackIpAdapterInfo->DhcpServer.IpAddress.String[0] = '\0';
    loopbackIpAdapterInfo->DhcpServer.IpMask.String[0] = '\0';
    loopbackIpAdapterInfo->DhcpServer.Context = 0;

    loopbackIpAdapterInfo->HaveWins = 0;

    loopbackIpAdapterInfo->PrimaryWinsServer.Next = nullptr;
    loopbackIpAdapterInfo->PrimaryWinsServer.IpAddress.String[0] = '\0';
    loopbackIpAdapterInfo->PrimaryWinsServer.IpMask.String[0] = '\0';
    loopbackIpAdapterInfo->PrimaryWinsServer.Context = 0;

    loopbackIpAdapterInfo->SecondaryWinsServer.Next = nullptr;
    loopbackIpAdapterInfo->SecondaryWinsServer.IpAddress.String[0] = '\0';
    loopbackIpAdapterInfo->SecondaryWinsServer.IpMask.String[0] = '\0';
    loopbackIpAdapterInfo->SecondaryWinsServer.Context = 0;

    loopbackIpAdapterInfo->LeaseObtained = 0;
    loopbackIpAdapterInfo->LeaseExpires = 103188859728;

    parentIpAdapterInfo->Next = loopbackIpAdapterInfo;
    }

    // DVM: E: Add loopback adapter

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,429

    Re: Why WSAStartup couldn't get the localhost ip address

    Quote Originally Posted by DennisVM-D2i View Post
    ...
    You might be looking for something like this:
    ...
    Please, use CODE tags (not the QUOTE ones!) while posting your code snippets. Also don't forget to properly format your code.
    Otherwise is looks absolutely unreadable.
    Victor Nijegorodov

  13. #13
    Join Date
    May 2024
    Posts
    4

    Re: Why WSAStartup couldn't get the localhost ip address

    The same post again, but as the editor did not seem to have a 'CODE' button, attempting to manually use CODE tags:

    Code:
    //  E.g.  For '127.4.0.2' (- as IPv4 'loopback'/localhost addresses are a class - Class A, '127.4.0.2' being one of them / a valid 'loopback'/localhost address; IPv6 should have reserved at least 10 'loopback'/localhost addresses instead of just the one  =S):
    
    #include "Iphlpapi.h"
    #include "Ipifcons.h"
    
    ...
    
                    // DVM: B:  Add loopback adapter
    
                    // ReSharper disable once CppLocalVariableMayBeConst
                    bool addLoopbackAdapter = true;
    
                    if (addLoopbackAdapter)
                    {
                        auto currIpAdapterInfo = m_pInfo;
                        IP_ADAPTER_INFO* parentIpAdapterInfo = nullptr;
    
                        while (currIpAdapterInfo != nullptr)
                        {
                            parentIpAdapterInfo =
                                currIpAdapterInfo;
    
                            currIpAdapterInfo =
                                currIpAdapterInfo->Next;
                        }
    
                        if (parentIpAdapterInfo != nullptr)
                        {
                            const auto loopbackIpAdapterInfo =
                                new IP_ADAPTER_INFO;
    
                            loopbackIpAdapterInfo->Next = nullptr;
    
                            loopbackIpAdapterInfo->ComboIndex = 999;
    
                            strcpy(loopbackIpAdapterInfo->AdapterName, _T("Loopback"));
    
                            strcpy(loopbackIpAdapterInfo->Description, _T("Loopback Pseudo-Interface"));
    
                            // Hw address length
                            loopbackIpAdapterInfo->AddressLength = 6;
    
                            // Hw address
                            // 2, 0, 76, 79, 79, 80, 0, 0 - '\x2', '\0', "LOOP", '\0', '\0'
                            //memset(loopbackIpAdapterInfo->Address, 0, sizeof(loopbackIpAdapterInfo->Address));
    
                            loopbackIpAdapterInfo->Address[0] = '\x2';
                            loopbackIpAdapterInfo->Address[1] = '\0';
                            loopbackIpAdapterInfo->Address[2] = 127;
                            loopbackIpAdapterInfo->Address[3] = 4;
                            loopbackIpAdapterInfo->Address[4] = 0;
                            loopbackIpAdapterInfo->Address[5] = 2;
                            loopbackIpAdapterInfo->Address[6] = 0;
                            loopbackIpAdapterInfo->Address[7] = 0;
    
                            //loopbackIpAdapterInfo->Address[0] = '\x2';
                            //loopbackIpAdapterInfo->Address[1] = '\0';
                            //loopbackIpAdapterInfo->Address[2] = 'L';
                            //loopbackIpAdapterInfo->Address[3] = 'O';
                            //loopbackIpAdapterInfo->Address[4] = 'O';
                            //loopbackIpAdapterInfo->Address[5] = 'P';
                            //loopbackIpAdapterInfo->Address[6] = '\0';
                            //loopbackIpAdapterInfo->Address[7] = '\0';
    
                            loopbackIpAdapterInfo->Index = 99;  // ???
                            loopbackIpAdapterInfo->Type = IF_TYPE_ETHERNET_CSMACD;
                            loopbackIpAdapterInfo->DhcpEnabled = 0;
                            loopbackIpAdapterInfo->CurrentIpAddress = nullptr;
    
                            loopbackIpAdapterInfo->IpAddressList.Next = nullptr;
                            strcpy(loopbackIpAdapterInfo->IpAddressList.IpAddress.String, _T("127.4.0.2"));
                            strcpy(loopbackIpAdapterInfo->IpAddressList.IpMask.String, _T("0.0.0.0"));
                            loopbackIpAdapterInfo->IpAddressList.Context = 0;
    
                            loopbackIpAdapterInfo->GatewayList.Next = nullptr;
                            strcpy(loopbackIpAdapterInfo->GatewayList.IpAddress.String, _T("0.0.0.0"));
                            strcpy(loopbackIpAdapterInfo->GatewayList.IpMask.String, _T("255.255.255.255"));
                            loopbackIpAdapterInfo->GatewayList.Context = 0;
    
                            loopbackIpAdapterInfo->DhcpServer.Next = nullptr;
                            loopbackIpAdapterInfo->DhcpServer.IpAddress.String[0] = '\0';
                            loopbackIpAdapterInfo->DhcpServer.IpMask.String[0] = '\0';
                            loopbackIpAdapterInfo->DhcpServer.Context = 0;
    
                            loopbackIpAdapterInfo->HaveWins = 0;
    
                            loopbackIpAdapterInfo->PrimaryWinsServer.Next = nullptr;
                            loopbackIpAdapterInfo->PrimaryWinsServer.IpAddress.String[0] = '\0';
                            loopbackIpAdapterInfo->PrimaryWinsServer.IpMask.String[0] = '\0';
                            loopbackIpAdapterInfo->PrimaryWinsServer.Context = 0;
    
                            loopbackIpAdapterInfo->SecondaryWinsServer.Next = nullptr;
                            loopbackIpAdapterInfo->SecondaryWinsServer.IpAddress.String[0] = '\0';
                            loopbackIpAdapterInfo->SecondaryWinsServer.IpMask.String[0] = '\0';
                            loopbackIpAdapterInfo->SecondaryWinsServer.Context = 0;
    
                            loopbackIpAdapterInfo->LeaseObtained = 0;
                            loopbackIpAdapterInfo->LeaseExpires = 103188859728;
    
                            parentIpAdapterInfo->Next = loopbackIpAdapterInfo;
                        }
                    }
    
                    // DVM: E:  Add loopback adapter

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured