-1

Possible Duplicate:
Check if a server is available

I'm writing a program in C# that queries the Windows servers on our domain, one after another. Currently the program will hang waiting for a reply if a server is offline or has a fault, what is the best way to wait and if no response is received move on to the next server? I've never had to do this before so any help is much appreciated.

Thanks Steve

Community
  • 1
  • 1
Steve Wood
  • 665
  • 5
  • 11
  • 15

2 Answers2

0

It sounds as though you want to take a look at BackgroundWorker and threading (the Thread class). I imagine you're blocking the UI thread by making whatever call it may be to check on your servers.

By using threading you can report back to the user exactly what is going on and apply your own timeouts if need be.

Daniel Frear
  • 1,459
  • 14
  • 22
0

You may ping your servers using PingReplay Class in C#:

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace PingTest
{
    public class PingExample
    {
        // args[0] can be an IPaddress or host name.
        public static void Main (string[] args)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();

            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
        }
    }
}

The code has been adopted from MSDN, see here.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62