I want to get the IP Addresses of all the machines connected to my PC using C#, but I don't want to use the Ping method because it takes a lot of time especially when the range of IP addresses is very wide.
From the desktop, navigate through; Start > Run> type "cmd.exe". A command prompt window will appear. At the prompt, type "ipconfig /all". All IP information for all network adapters in use by Windows will be displayed.
Starting with the simplest way to find someone's IP address is to use one of the many IP lookup tools available online. Resources such as WhatIsMyIPAddress.com or WhatIsMyIP.com offer tools to enter an IP address and search for its free public registry results.
using System.Net.NetworkInformation;
public static void ShowActiveTcpConnections()
{
Console.WriteLine("Active TCP Connections");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
Console.WriteLine("{0} <==> {1}",
c.LocalEndPoint.ToString(),
c.RemoteEndPoint.ToString());
}
}
source:
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getactivetcpconnections.aspx
Note: This only gives you the active TCP connections.
Shorter version of the code above.
foreach (var c in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections())
{
...
}
It might be better to do a ping sweep. Takes a few seconds to do 254 ip addresses. Solution is here https://stackoverflow.com/a/4042887/3645638
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With