Memduh Cüneyt
Memduh Cüneyt

Reputation: 53

How to get the IP addresses of the machines are connected to my PC using without using ping methode C#

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

Answers (1)

Svek
Svek

Reputation: 12858

Getting all the active TCP connections

Use the IPGlobalProperties.GetActiveTcpConnections Method

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())
{
    ...
}

Getting all the machines connected to a network

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

Related Questions