So I've implemented various approaches of the Ping Class including the most comprehensive one I found thanks to Vinjay B R. However, I can't seem to get any of the solutions to work in production. I can get it to work in VS 2015, but that's not saying much because I don't think the PING even leaves my network (I'm definitely not an expert in Networking so this is an assumption). I also had to implement the Split(":") approach below taking only array element 0 because if I don't, the IPAddress class captures an IP address with ":4321" on the tail end of the core IPAddress. I've tried the full address and the truncated address but neither approach works. I've used the cmd.exe Ping command with the truncated address to check both, I can only accomplish the ping with the truncated one so I've left the Split() in play. I'm at a loss, hopefully someone see's a elephant. My production server is Azure if that helps?
Currently it's throwing the PingException catch.
public class PingTest
{
public void Pinger()
{
SpeedTest Test = new SpeedTest();
IPAddress ip = new IPAddress();
var clientIP = ip.GetIPAddress();
string[] IPAddresses = clientIP.Split(':');
string address = IPAddresses[0];
//set the ping options, TTL 128
PingOptions pingOptions = new PingOptions(128, true);
//create a new ping instance
Ping ping = new Ping();
//32 byte buffer (create empty)
byte[] buffer = new byte[32];
//here we will ping the host 4 times (standard)
for (int i = 0; i < 4; i++)
{
try
{
//send the ping 4 times to the host and record the returned data.
//The Send() method expects 4 items:
//1) The IPAddress we are pinging
//2) The timeout value
//3) A buffer (our byte array)
//4) PingOptions
PingReply pingReply = ping.Send(address, 1000, buffer, pingOptions);
//make sure we dont have a null reply
if (!(pingReply == null))
{
switch (pingReply.Status)
{
case IPStatus.Success:
Test.Address = pingReply.Address.ToString();
Test.ResponseTime = pingReply.RoundtripTime.ToString() + " ms";
Test.Status = pingReply.Status.ToString();
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
Test.TestType = "Ping";
break;
case IPStatus.TimedOut:
Test.Address = pingReply.Address.ToString();
Test.ResponseTime = "";
Test.Status = "Connection has timed out...";
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
Test.TestType = "Ping";
break;
default:
Test.Address = pingReply.Address.ToString();
Test.ResponseTime = "";
Test.Status = pingReply.Status.ToString();
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
Test.TestType = "Ping";
break;
}
}
else
{
Test.Address = pingReply.Address.ToString();
Test.ResponseTime = "";
Test.Status = "Connection failed for an unknown reason...";
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
Test.TestType = "Ping";
}
}
catch (PingException ex)
{
Test.Address = address;
Test.ResponseTime = "";
Test.Status = string.Format("Connection Error: {0}", ex.Message);
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
Test.TestType = "Ping";
}
catch (SocketException ex)
{
Test.Address = address;
Test.ResponseTime = "";
Test.Status = string.Format("Connection Error: {0}", ex.Message);
Test.UserId = User.Identity.GetUserId();
Test.TestDate = DateTime.Now;
Test.TestType = "Ping";
}
}
using (ApplicationDbContext db = new ApplicationDbContext())
{
db.SpeedTest.Add(Test);
db.SaveChanges();
}
}
}
GET IP Address:
public class IPAddress
{
public string GetIPAddress()
{
//The X-Forwarded-For (XFF) HTTP header field is a de facto standard for identifying the originating IP address of a
//client connecting to a web server through an HTTP proxy or load balancer
String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return ip;
}
}