Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The fastest way to ping a large list of IP adresses?

I have a large list of IP addresses in a datatable and i have to ping them so quickly, I used this code :

public bool PingIP(string IP)
{
    bool result = false;
    try
    {                
        Ping ping = new Ping();
        PingReply pingReply = ping.Send(IP);
        if (pingReply.Status == IPStatus.Success)
            result = true;
    }
    catch
    {
        result = false;
    }
    return result;
}

then i call it in while loop :

private void CheckNetworkState()
{
    while (rowindexping > -1)
    {
        if (rowindexping == tbl_ClientInfo.Rows.Count)
        {
            rowindexping = -1;
            return;
        }

        string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString();
        if (!PingIP(ip))
        {
           do something
        }
        rowindexping++;
        Thread.Sleep(100);
    }
}

Since i want to do this work at the background of my project i call the class in a thread:

threadping = new Thread(CheckNetworkState);            
threadping.IsBackground = true;
threadping.Start(); 

my problem is that it takes so many time and does not work at the background. i mean the system is busy till all ip addresses in tbl_clientinfo go through the ping class. i want that system check all rows since i'm working with other part of my project.

Did i do correctly?

like image 415
maryam mohammadi Avatar asked Nov 05 '22 11:11

maryam mohammadi


1 Answers

Your code is running all the code on a single thread; you're not using multiple threads. Also, why do you have a Thread.Sleep in there?

Try the following:

  1. Query the database and get all the IPs
  2. In a loop, spin up a new thread that will run PingIP for each IP address

Note: You can also spin up a new thread every time you get a new row from the database

Sample:

    static void Main(string[] args)
    {
        // get the IPs from the database so you can iterate over them
        List<string> ips = new List<string>()
        {
            "google.com",
            "127.0.0.1",
            "stackoverflow.com"
        };

        foreach (var ip in ips)
        {
            // See http://stackoverflow.com/questions/4744630/unexpected-behaviour-for-threadpool-queueuserworkitem
            // for reason to use another variable in the loop
            string loopIp = ip;
            WaitCallback func = delegate(object state)
            {
                if (PingIP(loopIp))
                {
                    Console.WriteLine("Ping Success");
                }
                else
                {
                    Console.WriteLine("Ping Failed");
                }
            };
            ThreadPool.QueueUserWorkItem(func);
        }

        Console.ReadLine();
    }

    public static bool PingIP(string IP)
    {
        bool result = false;
        try
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(IP);

            if (pingReply.Status == IPStatus.Success)
                result = true;
        }
        catch
        {
            result = false;
        }

        return result;
    }
like image 78
Omar Avatar answered Nov 10 '22 04:11

Omar