Recently, due to the need for a management software for penetrating Internet cafes to restrict vswitches and vrouters. I set 10 IP addresses for the local machine to receive data from hardware.
I need to get all the IP addresses of the local machine.
Now, you have prepared and used it later.
Bool getlocalall_ipaddress (cstringarray & strarrayip)
{
Char szhostname [100];
Struct addrinfo * res;
Struct addrinfo * Next = NULL; Word version;
Wsadata data; version = makeword (0x02, 0x02);: wsastartup (version, & data );
If (socket_error =: gethostname (szhostname, sizeof (szhostname )))
{
: Wsacleanup (); Return false;
}
// The getaddrinfo API does not find the entry point in ws2_32.dll of Window 2000 and does not export this function.If (0! = Getaddrinfo (szhostname, null, null, & res ))
{
: Wsacleanup (); Return false;
}
/**//*
Considering that the data structure is compatible with multiple protocols, the MS does not directly write the following struct as struct sockaddr_in.
Struct sockaddr
{
U_short sa_family;
Char sa_data [14];
};
Struct sockaddr_in
{
Short sin_family;
U_short sin_port;
Struct in_addr sin_addr;
Char sin_zero [8];
};
*/Next = res; do
{
Strarrayip. Add (inet_ntoa (sockaddr_in *) (Next-> ai_addr)-> sin_addr); next = Next-> ai_next;} while (next! = NULL);: wsacleanup (); Return true;
}
Bool help: Network: getall_ipaddress2 (cstringarray & strarrayip)
{
Bool Bret;
Char szhostname [100];
Struct addrinfo * res;
Struct addrinfo * Next = NULL;
Word version;
Wsadata data;
Version = makeword (0x02, 0x02 );
: Wsastartup (version, & data );
If (socket_error =: gethostname (szhostname, sizeof (szhostname )))
{
: Wsacleanup ();
Return false;
}
Struct hostent * phostent = gethostbyname (szhostname );
If (phostent = NULL)
{
: Wsacleanup ();
Return false;
}
Int I = 0;
While (phostent-> h_addr_list [I]! = NULL)
{
In_addr in;
In. s_un.s_addr = * (DWORD *) (phostent-> h_addr_list [I]);
Strarrayip. Add (inet_ntoa (in ));
I ++;
}
: Wsacleanup ();
Return true;
}