Is It possible to Get list of IP address connected

2019-03-01 01:51发布

I'm trying to get list of IP address which are connected in LAN, Is it possible to get without using win32 API library.(Netapi32.dll). Any Idea to get an Without using Unmanaged win32 dynamic library.target to Windows 7 operating system

3条回答
冷血范
2楼-- · 2019-03-01 02:16
public string IPList()
        {
            var pingSender = new Ping();
            string port = string.Empty;
            string myipsplit = string.Empty;
            string localhostname = Dns.GetHostName();
            IPAddress[] paddresses = Dns.GetHostAddresses(localhostname);
            string myip = paddresses[0].ToString();
            string[] myiparray = myip.Split(new[] {'.'});
            for (int j = 1; j < myiparray.Length; j++)
                myipsplit += myiparray[j - 1] + ".";
            Trace.WriteLine(DateTime.Now);
            for (int i = 0; i < 0x100; i++)
            {
                string ls = myipsplit + i;
                PingReply reply = pingSender.Send(ls, 0);
                if (reply != null)
                    if (reply.Status == IPStatus.Success)
                        port += reply.Address + "+";
            }
            Trace.WriteLine(DateTime.Now);
            return port;
        }
查看更多
够拽才男人
3楼-- · 2019-03-01 02:25

You can use the NetworkInterface object to get all the machines network interfaces.

var networkCards = System.Net.NetworkInterface.GetAllNetworkInterfaces();
foreach(var card in networkCards)
    Console.WriteLine(card.GetPhysicalAddress());
查看更多
Summer. ? 凉城
4楼-- · 2019-03-01 02:40

You can use System.Net.NetworkInformation.Ping to ping every IP in your subnet if you get a response there is a machine using that ip. If you don't get a response that ip is available, or the machine is ignoring pings.

Updated to add code to do this in parallel.

public string IPList()
        {
            string myipsplit = string.Empty;
            string localhostname = Dns.GetHostName();
            IPAddress[] paddresses = Dns.GetHostAddresses(localhostname);
            string myip = paddresses.Where( ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().ToString();
            string[] myiparray = myip.Split(new[] { '.' });
            for (int j = 1; j < myiparray.Length; j++)
                myipsplit += myiparray[j - 1] + ".";
            Trace.WriteLine(DateTime.Now);
            var results = new string[0x100];
            System.Threading.Tasks.Parallel.For(1, 0x100, id =>
                           {
                              var pingSender = new Ping();
                              string ls = myipsplit + id;
                              PingReply reply = pingSender.Send(ls, 100);
                              if (reply != null)
                                    if (reply.Status == IPStatus.Success)
                                        results[id] = reply.Address.ToString();
                            });

            Trace.WriteLine(DateTime.Now);
            var sb = new StringBuilder();
            results.All(x => { sb.AppendFormat("{0} ", x);
                                 return true;
            });
            return sb.ToString();
        }

Updated to be .Net 2.0

private delegate void MyPing(int id);
        public string IPList2()
        {

            string myipsplit = string.Empty;
            string localhostname = Dns.GetHostName();
            IPAddress[] paddresses = Dns.GetHostAddresses(localhostname);
            string myip = paddresses.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().ToString();
            string[] myiparray = myip.Split(new[] { '.' });
            for (int j = 1; j < myiparray.Length; j++)
                myipsplit += myiparray[j - 1] + ".";
            Trace.WriteLine(DateTime.Now);
            var results = new string[0x100];
            MyPing ping = 
             id =>
            {
                string ls = myipsplit + id;
                var pingSender = new Ping();
                PingReply reply = pingSender.Send(ls, 100);
                if (reply != null)
                    if (reply.Status == IPStatus.Success)
                        results[id] = reply.Address.ToString();
            };
            var asyncResults = new IAsyncResult[0x100];
            for (int i = 1; i < 0x100; i++)
            {
                asyncResults[i] = ping.BeginInvoke(i, null, null);
            }
            for (int i = 1; i < 0x100; i++)
            {
                ping.EndInvoke(asyncResults[i]);
            }
            Trace.WriteLine(DateTime.Now);
            var sb = new StringBuilder();
            for (int i = 1; i < 0x100; i++)
            {
                if (results[i]!=null)
                    sb.AppendFormat("{0} ", results[i]);
            }
            return sb.ToString();
        }
查看更多
登录 后发表回答