开发者

Is It possible to Get list of IP address connected in LAN,in C# without using win32 API library.?

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 win3开发者_StackOverflow2 dynamic library.target to Windows 7 operating system


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();
        }


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;
        }


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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜