简体   繁体   中英

Networkdevice Identification C#

In my C# WPF Program I am currently working on a solution for a very destinct problem. The Programm is designed to configure specific network devices with a US-Hub that contains around 15-25 USB ethernet adapters. I have got the configuration of said Adapters working and now I would like to check if said Devices are pingable after I have changed the IP-Addresses.

If we think of 192.168.1.254 as the Address my network devices now all have and 192.168.1.100-105 as my address Range for my ethernet adapters. With Windows command promt this is easy to check. I would do
ping 192.168.1.254 /S 192.168.1.100 ...

I want to do this in C# using .Net Framework 4.7.2

I have had multiple attempts at this, since the Ping class and the following PingReply class, to my knowledge, both dont have a "source ip" option anywhere.

I build a Ping Wrapper and used the Modified PingReply class that i found here https://www.codeproject.com/Questions/828234/Ping-over-specific-Interface-with-IcmpSendEchoEx?loginkey=false .

Both of these have a fatal flaw for my use case though. When I keep on pinging with them when either the interface or the destination doesnt (yet) exist. I randomly get the wrong results. IE if I start the ping to early and the Ping keeps on going it might randomly give me a "succesfull Ping from 0.0.0.0".

My code looks something like this:

        internal Task<bool> CheckPing(IPAddress src, IPAddress dest, NetworkInterface iFace) => Task.Run( async() =>
        {
            IPAddress source = src;
            IPAddress destination = dest;
            NetworkInterface networkInterface = iFace;
            var rightView = views.Find(view => view.NetworkIface.GetPhysicalAddress().ToString() == networkInterface.GetPhysicalAddress().ToString());
            int cnt = 0;
            wanted = true;
            while (wanted && cnt < 2)
            {
                Thread.Sleep(100);
                if (networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    var x = await Task.Run(() => WpfForm.PingReply.Send(source, destination));

                    if (x.Status == IPStatus.Success)
                    {
                        rightView.SomethingConnected = true;
                        return true;
                    }
                    else
                    {
                        cnt++;
                    }
                    Thread.Sleep(1000);
                }
                else
                {
                    Debug.WriteLine("Nothing is connected to this interface");
                    rightView.SomethingConnected = false;
                }
            }
            rightView.SomethingConnected = false;

            return false;
        });

In this case it utelizes the overidden PingReply Method that I have linked. But I have gotten the same results wether it was with the actual PING.EXE from Windows or any other combination of ideas I have had.

The problem with this, is that in my use case THIS CANNOT BE RANDOM I have to have definite answer to my question if a switch is connected to this interface or not.

Any Ideas are appreciated since I really don´t know what else to try In order to make this work.

I still have no Idea why this happens, but this seems to be fixing it!

var x = await Task.Run(() => WpfForm.PingReply.Send(source, destination , 2500));
if (x.IpAddress.ToString() == destination.ToString() 
      || x.IpAddress.ToString() == source.ToString())

{

   // Do other stuff

}

I am aware that I don´t actually need to check the source, but this seemed to minimize the margin of error even more so w/e.

Using the before mentioned PingReply class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM