Files
T-DAS/Common/PingHelper.cs
2023-01-13 15:30:20 +08:00

67 lines
2.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace Epost.Common
{
public class PingHelper
{
public bool pingSender(string ip, int time)
{
Ping ping = new Ping();
PingReply reply = ping.Send(ip, time);//第一个参数为ip地址第二个参数为ping的时间 120
if (reply.Status == IPStatus.Success)
{
return true;
//ping的通
}
else
{
return false;
//ping不通
}
}
public void getmac()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
//远程服务器ip
string remoteHostNameAddress = "192.168.3.137";
//构造Ping实例
Ping pingSender = new Ping();
//Ping选项设置
PingOptions options = new PingOptions();
options.DontFragment = true;
//测试数据
string data = "test data abcabc";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//设置超时时间
int timeout = 120;
//调用同步send方法发送数据将返回结果保存至PingReply实例
PingReply reply = pingSender.Send(remoteHostNameAddress, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
foreach (NetworkInterface adapter in adapters)
{
string address = "答复的主机地址:" + reply.Address.ToString();
string RoundtripTime = "往返时间:" + reply.RoundtripTime;
string Ttl = "生存时间TTL" + reply.Options.Ttl;
string GetPhysicalAddress = "MAC地址" + adapter.GetPhysicalAddress();
}
}
else
{
}
}
}
}