usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacepingdemo{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Please enter IP:"); varIP =Console.ReadLine (); varResutl =Ping (IP); Console.WriteLine (RESUTL); Console.ReadLine (); } /// <summary> ///whether the specified host can be Ping/// </summary> /// <param name= "IP" >IP address or host name or domain name</param> /// <returns>true Pass, false not pass</returns> Static BOOLPing (stringIP) { Try{System.Net.NetworkInformation.Ping P=NewSystem.Net.NetworkInformation.Ping (); System.Net.NetworkInformation.PingOptions Options=NewSystem.Net.NetworkInformation.PingOptions (); Options. Dontfragment=true; stringdata ="Test data!"; byte[] buffer =Encoding.ASCII.GetBytes (data); intTimeout = the;//timeout time, unit: millisecondsSystem.Net.NetworkInformation.PingReply reply =p.send (IP, timeout, buffer, options); if(Reply = =NULL|| Reply. Status = =System.Net.NetworkInformation.IPStatus.Success)return true; return false; } Catch(System.Net.NetworkInformation.PingException e) {Throw NewException ("the server could not be found"); } } }}
C # Implementing Ping server