.NET Diary

March 19, 2013

Ping Server from .NET C# Application to check up/down status

Filed under: All, C# — Tags: — leoullas @ 2:33 pm

internal bool PingServer(string IpAddress)
{
string strDot = “.”;
string[] retWords = Split(IpAddress, strDot.ToCharArray());

bool netOK = false;

byte[] AddrBytes = new byte[] { Convert.ToByte(retWords[0]),
Convert.ToByte(retWords[1]),
Convert.ToByte(retWords[2]),
Convert.ToByte(retWords[3]) }; // byte array for server address.

using (System.Net.NetworkInformation.Ping png = new System.Net.NetworkInformation.Ping())
{
System.Net.IPAddress addr;

addr = new System.Net.IPAddress(AddrBytes);

try
{
netOK = (png.Send(addr, 1500, new byte[] { 0, 1, 2, 3 }).Status == IPStatus.Success);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
netOK = false;
}
return netOK;
}
}

 

internal string[] Split(string sourceWord, char[] splitter)
{
string[] retWords = sourceWord.Split(splitter);
return retWords;
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: