2

From C# console application i had open the command prompt and checked the ping utility.

string aptracommand;
aptracommand = "/C ping 10.38.2.73";
Process.Start(@"cmd",aptracommand);

Now , i need to apply a conditional statement if ping request time out then it should say "Not able to connect" and if its able to ping then need to show "Server is up"

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
Sohail
  • 780
  • 3
  • 14
  • 27

3 Answers3

9

You can use Ping class for this purpose. As stated below:

using System.Net.NetworkInformation;

var ping = new Ping();
var reply = ping.Send("10.38.2.73", 60 * 1000); // 1 minute time out (in ms)
if (reply.Status == IPStatus.Success)
{
   Console.WriteLine("Server is up");
}
else
{
   Console.WriteLine("Server is down");
}

You can reduce the time out value to quickly check if server is up or down.

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • when I have done this in the console window nothing its showing , just its getting closed after the given time – Sohail Sep 27 '12 at 13:43
  • thank you I have got the answer, just added console.readline and i was able to see the answer. thank you – Sohail Sep 27 '12 at 13:55
2

use this instead

Ping pingSender = new Ping ();
byte[] data = Encoding.ASCII.GetBytes ("test");
int timeout = 100;
PingReply reply = pingSender.Send("127.0.0.1", timeout, data);
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);
}
Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
0

I think you will need to capture the output, check for the existence of time out string and then take the decision based on that.

Capturing the Output

Community
  • 1
  • 1
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138