Obtain all IP addresses of the local machine.

Source: Internet
Author: User

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;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.