Get local IP address
To get local Ip Address:
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
To check if you're connected or not:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
How to get the local IP of the PC?
You are missing to filter the IP by type (v4). Anyway you can have multiple IP v4 addresses on your PC (for example you can have 2 interfaces, LAN and Wi-Fi).
The following code gets the list of available IP v4.
List<string> ips = new List<string>();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in entry.AddressList)
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
ips.Add(ip.ToString());
How do I get the Local Network IP address of a computer programmatically?
In How to get IP addresses in .NET with a host name by John Spano, it says to add the System.Net
namespace, and use the following code:
//To get the local IP address
string sHostName = Dns.GetHostName ();
IPHostEntry ipE = Dns.GetHostByName (sHostName);
IPAddress [] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ());
}
Getting the IP address of the current machine using Java
import java.net.DatagramSocket;
import java.net.InetAddress;
try(final DatagramSocket socket = new DatagramSocket()){
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
ip = socket.getLocalAddress().getHostAddress();
}
This way works well when there are multiple network interfaces. It always returns the preferred outbound IP. The destination 8.8.8.8
is not needed to be reachable.
Connect
on a UDP socket has the following effect: it sets the destination for Send/Recv, discards all packets from other addresses, and - which is what we use - transfers the socket into "connected" state, settings its appropriate fields. This includes checking the existence of the route to the destination according to the system's routing table and setting the local endpoint accordingly. The last part seems to be undocumented officially but it looks like an integral trait of Berkeley sockets API (a side effect of UDP "connected" state) that works reliably in both Windows and Linux across versions and distributions.
So, this method will give the local address that would be used to connect to the specified remote host. There is no real connection established, hence the specified remote ip can be unreachable.
Edit:
As @macomgil says, for MacOS you can do this:
Socket socket = new Socket();
socket.connect(new InetSocketAddress("google.com", 80));
System.out.println(socket.getLocalAddress());
How to get a list of all valid IP addresses in a local network?
Install nmap,
sudo apt-get install nmap
then
nmap -sP 192.168.1.*
or more commonly
nmap -sn 192.168.1.0/24
will scan the entire .1 to .254 range
This does a simple ping scan in the entire subnet to see which hosts are online.
How do I get the local IP address in Go?
You need to loop through all network interfaces
ifaces, err := net.Interfaces()
// handle err
for _, i := range ifaces {
addrs, err := i.Addrs()
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
// process IP address
}
}
Play (taken from util/helper.go)
Related Topics
How to Write a Short Literal in C++
Does Const-Correctness Give the Compiler More Room For Optimization
What Is _Gxx_Personality_V0 For
How to Create My Own Comparator For a Map
Why Is Default Constructor Called in Virtual Inheritance
How to Parse a Date String into a C++11 Std::Chrono Time_Point or Similar
Vectors, Structs and Std::Find
Deleting a Pointer to Const (T Const*)
Opencv Get Pixel Channel Value from Mat Image
Is Calling Destructor Manually Always a Sign of Bad Design
How to Implement Make_Unique Function in C++11
In C++ How to Go to a Specific Line in a Text File
Generate Random Double Numbers in C++