Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all,

I am Looking to scan a network connected computer to my system along with all subnet mask created on that IP address in web application,

I have a code that give me a list in console application using Ping method but that method does not work in web application, so need some help.

Here is console Code

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

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

                Ping p = new Ping();
                p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
        
                p.SendAsync(ip, 100, ip);
            }
        
            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);
            }

        }
    }



Thanks
Amit
Posted
Updated 11-Jan-12 19:37pm
v3
Comments
Tejas Vaishnav 12-Jan-12 1:05am    
Provide that code, so we will try to modify for you....

Why you are using the ping method to scan all IP address,
see the below code, this will help you

IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
         IPAddress [] addr = ipEntry.AddressList;

         for (int i = 0; i < addr.Length; i++)
         {
             Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
         }


See this Link[^] also.

Thanks
 
Share this answer
 
Comments
amit28august 12-Jan-12 1:47am    
I tried this but it gives me only local host name and IP, thats it, It is not all network host connected and only for associated IP list for that host.
Thanks to all, I got the solution for it, thanks a lot for postings.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900