Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to do a list of devices on the local network ?

SuperChocolatineSuperChocolatine Member ✭✭
edited August 2020 in Xamarin.Forms

Hello,
I m working on a home automation project and I have to do an application.
To be in communication with a hub I must scan the local network to detect him.
I tried to do a ping test but I had a timeout for every devices.
I tried to send a request to the hub but after a few request I have a strange exeptions.
After, I tried a ping with shell but the devices don't replied.
Have you an idea ?
Thank You

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    I tried to do a ping test but I had a timeout for every devices.
    I tried to send a request to the hub but after a few request I have a strange exeptions.
    After, I tried a ping with shell but the devices don't replied.

    According to your description, it is difficult for us to locate the problem.
    How did you scan the local network ? Is there any detailed error log about your question?

  • SuperChocolatineSuperChocolatine Member ✭✭
    edited August 2020

    @jezh said:

    I tried to do a ping test but I had a timeout for every devices.
    I tried to send a request to the hub but after a few request I have a strange exeptions.
    After, I tried a ping with shell but the devices don't replied.

    According to your description, it is difficult for us to locate the problem.
    How did you scan the local network ? Is there any detailed error log about your question?

    After few tests and adjustements, the scan worked but not every devices are correctly ping.
    The problem was I tried the scan on the emulator and like it's a virtual local network I did'nt find my devices.
    So I tried on my smartphone and not every devices are ping.
    On the hub for example he replied me a Timeout Error.
    I tested with the terminal of my computer and the ping worked.
    Here is my code that do the scan:

    `public static class ScanLocalNetwork
    {
    public static List hubs = new List();
    public static bool IsOk = false;
    private static int j = 6;
    private static int i = 2;
    public static void Ping_all()
    {
    j = 6;
    for (i = 2; i <= 255; i++)
    {
    string ping_var = "192" + "." + "168" + "." + "1" + "." + i;
    Console.WriteLine(ping_var);
    Ping(ping_var, 1, 14000);

            }
    
        }
    
        private static void Ping(string host, int attempts, int timeout)
        {
            for (int i = 0; i < attempts; i++)
            {
                new Thread(delegate ()
                {
                    try
                    {
                        Ping ping = new Ping();
                        ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
                        ping.SendAsync(host, timeout, host);
                    }
                    catch
                    {
    
                    }
                }).Start();
            }
        }
        private static void PingCompleted(object sender, PingCompletedEventArgs e)
        {
            string ip = (string)e.UserState;
            if (e.Reply != null && e.Reply.Status == IPStatus.Success)
            {
                string hostname = GetHostName(ip) + " " + ip;
                if (hostname == null)
                {
                    hostname = "unamed";
                }
    
                 ScanLocalNetwork.hubs.Add(new HubModel() { Ip = ip, Name = hostname });
    
            }
            j++;
            Console.WriteLine("j: " + j.ToString() + " i: " + i.ToString()); 
            IsOk = (i == j) ? true : false;
    
        }
    
        private static string GetHostName(string ipAddress)
        {
            try
            {
                IPHostEntry entry = Dns.GetHostEntry(ipAddress);
                if (entry != null)
                {
                    return entry.HostName;
                }
            }
            catch (SocketException)
            {
            }
    
            return null;
        }
    }`
    

    Thank you,

  • justinkingstjustinkingst Member ✭✭

    Do you have found solution? I have the same problem like you!

  • @justinkingst said:

    Do you have found solution? I have the same problem like you!

    Sorry, I use the same code since I have got this problem, sorry I can't help you.

Sign In or Register to comment.