C++/CLI Code Snippet - Get local computer IP address
C++/CLI code snippet get the IP address from the local machine. For computers with multiple IP addresses with one or more NIC cards can use IP address object list to obtain all the IP addresses.
Bookmark:
C++/CLI Code Snippet - Get local computer IP address
This .Net C++/CLI code snippet get the IP address from the local machine. This function uses System.Net namespace to get local machine hostname and then get a array of IPAddress objects. Modify the exception handling section to meet your project requirements.
/// <summary> /// Gets IP addresses of the local computer /// </summary> System::String ^GetLocalIP() { System::String ^_IP = nullptr; // Resolves a host name or IP address to an IPHostEntry instance. // IPHostEntry - Provides a container class for Internet host address information. System::Net::IPHostEntry ^_IPHostEntry = System::Net::Dns::GetHostEntry(System::Net::Dns::GetHostName()); // IPAddress class contains the address of a computer on an IP network. for each (System::Net::IPAddress ^_IPAddress in _IPHostEntry->AddressList) { // InterNetwork indicates that an IP version 4 address is expected // when a Socket connects to an endpoint if (_IPAddress->AddressFamily.ToString() == "InterNetwork") { _IP = _IPAddress->ToString(); } } return _IP; }
Here is a simple example showing how to use above function (GetLocalIP) to display local machine IP address.
Console::WriteLine("Local computer IP address : " + GetLocalIP());
This .Net code snippet gets the array of all the IPAddress objects. Use this method if you are writing sockets applications, a computer can have multiple IP addresses with one or more NIC cards.
/// <summary> /// Gets IP addresses object list from the local computer /// </summary> array<System::Net::IPAddress^> ^GetLocalIPList() { // Resolves a host name or IP address to an IPHostEntry instance. // IPHostEntry - Provides a container class for Internet host address information. System::Net::IPHostEntry ^_IPHostEntry = System::Net::Dns::GetHostEntry(System::Net::Dns::GetHostName()); // return a list of IP addresses that are associated with a host. return _IPHostEntry->AddressList; }
Here is a simple example showing how to use above function (GetLocalIPList) to display all the IP addresses from local machine.
// display all IP addresses of local machine for each (System::Net::IPAddress ^_IPAddress in GetLocalIPList()) Console::WriteLine(_IPAddress->ToString()); // display all IP addresses of local machine with addressing scheme for each (System::Net::IPAddress ^_IPAddress in GetLocalIPList()) Console::WriteLine(_IPAddress->AddressFamily.ToString() + " = " + _IPAddress->ToString());
C++/CLI Keywords Used:
- DNS
- IPAddress
- GetHostEntry
- IPHostEntry
- AddressList
- AddressFamily
- InterNetwork
- Exception
Code Snippet Information:
- Applies To: Visual Studio, .Net, C++, CLI, DNS, Domain Name System, IPAddress, GetHostEntry, IPHostEntry, AddressFamily, InterNetwork, Socket programming, Networking, Network monitoring
- Programming Language : C++/CLI
External Resources: