Reputation: 53
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.
Upvotes: 5
Views: 409
Reputation: 12858
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());
}
}
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
Upvotes: 7