Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# check domain is live

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

like image 893
Data-Base Avatar asked Dec 28 '22 05:12

Data-Base


2 Answers

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.

like image 77
JoeBilly Avatar answered Jan 12 '23 09:01

JoeBilly


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);
}
like image 40
Albin Sunnanbo Avatar answered Jan 12 '23 11:01

Albin Sunnanbo