Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi have a good time
i need to find and get availble IPs in my system(LAN, Wireless,...).
i work with visual c++ 2010 MFC template.
thanks a lot.
by
Posted
Comments
Sergey Alexandrovich Kryukov 25-Oct-11 2:56am    
What IP do you call "available"? Keep in mind, if you assign IP to some host and add it to the network, you cannot detect it if the host shows no network activity. Do you want to probe all possible IPs? Depending on the network class, could be too many of them... :-)
--SA

Firstly you should know your IP address and Networkmask because you can only guess IP addresses which are resided in your subnet Id.


And consider that there may be other IP addresses which could not be guessed and are accessible in your network and only network administrator knows about them.
Another way to identify all of available networks is sniffing Routing packets which have information about available networks, that its probability is very low to obtain them and also the time that it takes to implement this scenario is very long.

For obtaining IP and subnet mask this is a good solution :
http://stackoverflow.com/questions/2989419/how-can-i-programmatically-find-the-ip-address-netmask-gateway-configured-for-a[^]

And do not forget about Variable Length Subnet Masks which are very important in guessing your subnet possible IP addresses.

Good Luck
 
Share this answer
 
Please see my comment to the question. For example, you can ping all addresses 192.168.0.*; see http://www.petri.co.il/quickly_find_used_ip_addresses.htm[^].

The probed hosts need at least to implement ICMP, http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol[^].

How to ping in C++? For example, see http://www.developerfusion.com/article/4628/how-to-ping/[^].

—SA
 
Share this answer
 
v2
Comments
[no name] 25-Oct-11 3:40am    
hithank you for your solution but i need a way (function) to check the IPs.
are they exist or Not?
that code just ping them in system(CMD).
thank you.
This one has been on the net for ages , I dunno where I got it but all the credits go to another poster , somewhere . It gets all the IP's related to all/any network adapters.

C++
int GetIPs(int, char **)
{
    char ac[80];
    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
        cerr << "Error " << WSAGetLastError() <<
                " when getting local host name." << endl;
        return 1;
    }
    cout << "Host name is " << ac << "." << endl;

    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        cerr << "Bad host lookup." << endl;
        return 1;
    }

    for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
    }
    return 0;
}

int main(int argc, char *argv[])
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }

    int retval = GetIPs(argc, argv);

    WSACleanup();
    return retval;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900