Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# ping all machines on local network

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace ConsoleApplication1
{
    class Program
    {
        static CountdownEvent countdown;
        static int upCount = 0;
        static object lockObj = new object();
        const bool resolveNames = true;

        static void Main(string[] args)
        {
            countdown = new CountdownEvent(1);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string ipBase = "10.22.4.";
            for (int i = 1; i < 255; i++)
            {
                string ip = ipBase + i.ToString();

                Ping p = new Ping();
                p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
                countdown.AddCount();
                p.SendAsync(ip, 100, ip);
            }
            countdown.Signal();
            countdown.Wait();
            sw.Stop();
            TimeSpan span = new TimeSpan(sw.ElapsedTicks);
            Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount);
            Console.ReadLine();
        }

        static void p_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            string ip = (string)e.UserState;
            if (e.Reply != null && e.Reply.Status == IPStatus.Success)
            {
                if (resolveNames)
                {
                    string name;
                    try
                    {
                        IPHostEntry hostEntry = Dns.GetHostEntry(ip);
                        name = hostEntry.HostName;
                    }
                    catch (SocketException ex)
                    {
                        name = "?";
                    }
                    Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);
                }
                else
                {
                    Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
                }
                lock(lockObj)
                {
                    upCount++;
                }
            }
            else if (e.Reply == null)
            {
                Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
            }
            countdown.Signal();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Bedingungen in C# – if, else und else if 
Csharp :: function to accept interger 
Csharp :: call class c# 
Csharp :: C# multiple button click event to textbox 
Csharp :: animatro set bool unity 
Csharp :: c# fieldnullexception 
Csharp :: html inside razor 
Csharp :: messagebox error c# 
Csharp :: HOW TO CALL AN EXTENSION METHOD FOR VIEW C# 
Csharp :: c# multipthreading 
Csharp :: c# panel to graphics 
Csharp :: C# dest 
Csharp :: c# UserControl make background transparent 
Csharp :: show double in textbox c# 
Csharp :: make first 2 words upper case c# 
Csharp :: multiple input same line c# 
Csharp :: add auto mapper in startup 
Csharp :: cors denied error in asp.net core 
Csharp :: Return out of a Ienumerator/Courotine in C# 
Csharp :: Fix Array outside the bonus 
Csharp :: sequelize instance method is not a function 
Csharp :: xunit setup throw exception 
Csharp :: #StopRape 
Csharp :: Startup.cs file 
Csharp :: add two large numbers 
Csharp :: is and as in c# 
Csharp :: unity wrap around value 
Csharp :: read barcode with barcode scanner c# winform serial port number 
Csharp :: string in char list f# 
Csharp :: c# enum variable set to nonthing 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =