5

I want to know how I can check if the domain is a live in c#?

I made this

IPAddress[] addresslist = Dns.GetHostAddresses("domain");

it returns the IP(s), which is good, but I want to check if it is live (like ping)?

I just need to know if the PC is in the same network as the server, so I have a function to check but the functions based on the code above, and it return IPs but I noticed it is based on the cached DNS!

I want to add another check function to see if the server is online!

cheers

Data-Base
  • 8,418
  • 36
  • 74
  • 98
  • 2
    What is "live" to you? That you can ping it? That is has a webserver that is running? – Albin Sunnanbo Nov 04 '10 at 07:39
  • that the server is up and running, simply the program will check the domain and see if it is up, then (I have this done) it will check the IP address matches with the original, simply it is a checking mechanism for an internal app that it will preform some task only if the machine is connected to our network at work – Data-Base Nov 04 '10 at 08:06
  • @Data-Base - would it be bad if it worked when not connected to your network? Because hosts and IP addresses are easily cloned/spoofed. – Damien_The_Unbeliever Nov 04 '10 at 08:09
  • @Damien_The_Unbeliever, no it is not bad, it is just a simple app that we build to do some task, not a critical! – Data-Base Nov 04 '10 at 08:19
  • @Data-Base, you have added a bounty, is there anything you miss in the answers below? – Albin Sunnanbo Nov 15 '10 at 18:38
  • Try running an LDAP query on the domain controller - if you get an exception, then the domain controller is hosed. – Steven Evers Nov 17 '10 at 15:12

3 Answers3

6

The System.Net.NetworkInformation.OperationalStatus Property allow you to detect if your network interface is connected (up).

The NetworkInterface.GetIPProperties Method will give you the IP informations.

You can use this to check if the computer is on lan, then do what you need -like a ping- if connected.

Chiramisu
  • 4,687
  • 7
  • 47
  • 77
JoeBilly
  • 3,057
  • 29
  • 35
5

You can use the Ping.Send(IPAddress) method.

Edit: Or one of the overloads that takes the hostname directly. Ping.Send(string)

Example from MSDN:

Ping pingSender = new Ping ();
PingReply reply = pingSender.Send ("www.contoso.com");
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);
}
else
{
    Console.WriteLine (reply.Status);
}
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Hi, Thanks, but the else does not work for me! it just show nothing – Data-Base Nov 04 '10 at 07:57
  • @Data-Base, with the exact example and the hostname above I get `TimedOut` on my console. What happens if you put a breakpoint in the else-part? – Albin Sunnanbo Nov 04 '10 at 09:08
  • funny, now it worked with the example above ("www.contoso.com") but when I use our domain it does work only when my PC is connected to the network, when I disconnect the cable then it does not sjow the timeout thing! !!! – Data-Base Nov 04 '10 at 09:26
  • Don't forget the firewall on the target may be blocking ping requests. – John Warlow Nov 12 '10 at 11:12
  • still, it is good, that gives the app a message not to run some functions, which is good – Data-Base Nov 12 '10 at 11:38
  • the idea is to check if the PC is on the same line where there is a server with an IP(s) and a domain name (hostname) if the app can not reach the server (for what ever reason) and the domian does not match the IP then it will run some functions, nothing fancy – Data-Base Nov 12 '10 at 11:44
0

If you need to perform any kind of task with a remote system, then no amount of pre-checking will ensure that you're able to carry out the task - networks and hosts are not 100% reliable, and what was true during your checking operations may no longer be true when attempting the actual task, or throughout the duration of performing the task.

The only sane thing to do is to attempt the task you need to perform, and deal with errors, timeouts, etc, in a safe manner. This may be letting the user know of the failure, or retrying it (possibly with increasing delays between each attempt), or just logging the error somewhere.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • Thanks, I just need to know if some host is live! and if it is then the program will work (it is an app for our internal use at work), cheers – Data-Base Nov 04 '10 at 07:59