2014-04-01 01:18:51 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2021-07-29 15:22:51 +12:00
|
|
|
|
Copyright (c) 2007-2021 ShareX Team
|
2014-04-01 01:18:51 +13:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-04-01 01:18:51 +13:00
|
|
|
|
{
|
|
|
|
|
public static class PingHelper
|
|
|
|
|
{
|
|
|
|
|
public static PingResult PingHost(string host, int timeout = 1000, int pingCount = 4, int waitTime = 100)
|
|
|
|
|
{
|
|
|
|
|
PingResult pingResult = new PingResult();
|
|
|
|
|
IPAddress address = GetIpFromHost(host);
|
|
|
|
|
byte[] buffer = new byte[32];
|
|
|
|
|
PingOptions pingOptions = new PingOptions(128, true);
|
|
|
|
|
|
|
|
|
|
using (Ping ping = new Ping())
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < pingCount; i++)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
PingReply pingReply = ping.Send(address, timeout, buffer, pingOptions);
|
|
|
|
|
|
|
|
|
|
if (pingReply != null)
|
|
|
|
|
{
|
|
|
|
|
pingResult.PingReplyList.Add(pingReply);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (waitTime > 0 && i + 1 < pingCount)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(waitTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pingResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IPAddress GetIpFromHost(string host)
|
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
if (!IPAddress.TryParse(host, out IPAddress address))
|
2014-04-01 01:18:51 +13:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
address = Dns.GetHostEntry(host).AddressList[0];
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return address;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|