Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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.

like image 410
Memduh Cüneyt Avatar asked May 31 '17 06:05

Memduh Cüneyt


People also ask

How do I find the IP address of a device using CMD?

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.

How can I find the IP address of someone else's computer?

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.


1 Answers

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

like image 55
Svek Avatar answered Oct 09 '22 01:10

Svek