Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

c# - How to do network Ping .NET

I did a small program that ping my entire network, but I think I have a problem because some equipment stop working (it runs every 2 minutes and takes 1 minute to complete the operation), the question is: ?Is there a better way to do this?

Here the code:

Console.WriteLine("Haciendo ping a los equipos, no cierre esta ventana... ");
Ping ping = new Ping();
byte[] buffer = new byte[32];
PingOptions pingoptns = new PingOptions(128, true);
int timeout = Convert.ToInt32(ConfigurationManager.AppSettings["timeout"].ToString());
List<Equipo> list_Eq = new List<Equipo>();
DataTable dt = DB.ShowData("select ip from testping where estatus = 1");

// Por cada IP se realiza ping y se  agrega a la lista del modelo
foreach (DataRow item in dt.Rows)
{

    if (ping.Send(item[0].ToString(), timeout, buffer, pingoptns).Status == IPStatus.Success)
    {
        list_Eq.Add(new Equipo
        {
            ip = item[0].ToString(),
            estado = 1
        });
    }
    else
    {
        list_Eq.Add(new Equipo
        {
            ip = item[0].ToString(),
            estado = 0
        });
    }
}

// Se actualiza el estado de las ip segun la respuesta del ping
foreach (var eq in list_Eq)
{
    DB.ExecQuery("update testping set estado = " + eq.estado + " where ip = '" + eq.ip + "'");
}

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm trying this way (the code is getting better)

    using System;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Configuration;
using System.Threading.Tasks;

namespace pingtask
{
    class Program
    {
        static void Main(string[] args)
        {

            Task task = new Task(makePing);
            task.Start();
            task.Wait();
            Console.WriteLine("Test finished.");
            Console.ReadLine();

        }

        static async void makePing()
        {

            Ping ping = new Ping();
            byte[] buffer = new byte[32];
            PingOptions pingoptns = new PingOptions(128, true);
            int timeOut = 4000;

            List<string> list_Eq = new List<string>();
            list_Eq.Add("192.168.23.33");
            list_Eq.Add("192.168.0.11");
            list_Eq.Add("192.168.0.7");
            list_Eq.Add("192.168.0.8");
            list_Eq.Add("192.168.0.9");
            list_Eq.Add("192.168.0.5");
            list_Eq.Add("192.168.0.1");
            list_Eq.Add("192.168.0.2");
            list_Eq.Add("192.168.0.3");
            list_Eq.Add("192.168.0.10");
            list_Eq.Add("192.168.0.14");
            list_Eq.Add("192.168.0.4");

            foreach (var item in list_Eq)
            {
                Console.WriteLine("ADDRESS:" + item);
                PingReply reply = await ping.SendPingAsync(item, timeOut, buffer, pingoptns);
                Console.WriteLine("TIME:" + reply.RoundtripTime);
                Console.WriteLine("==============================");
            }

        }
    }
}

Now I have a doubt, my code is something like "ping -n 1"? I mean if I want to do "ping -n 4" I should do it into a loop?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...