• How to Ping a Server using C#

    Posted on May 8, 2013 by in Dotnet Tips & Tricks, Tips & Tricks

    Namespaces:

    using System;
    using System.Net;
    using System.Net.NetworkInformation;
    
    public static void PingServer(string hostName)
    {
        //Allows an application to determine whether a remote computer is accessible
        //over the network.
        Ping ping = new Ping();
    
        //Attempts to send an Internet Control Message Protocol (ICMP) echo message
        //to the specified computer
        PingReply reply = ping.Send(hostName);
    
        if (reply.Status != IPStatus.Success)
            throw new ApplicationException(reply.Status.ToString() + " : server not responding.");
    
        Console.WriteLine("Host Name : " + hostName); 
        Console.WriteLine("IP Address : " + reply.Address.ToString());
    }

    Usage:
    PingServer(“www.google.com”)

    How to Ping a Server using C#


    Be Sociable, Share!
      Post Tagged with , ,

    Written by

    View all articles by

    Email : [email protected]

    Leave a Reply