本文整理汇总了C#中System.Net.NetworkInformation.Ping.SendAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Ping.SendAsync方法的具体用法?C# Ping.SendAsync怎么用?C# Ping.SendAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.NetworkInformation.Ping
的用法示例。
在下文中一共展示了Ping.SendAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ListAllHosts
public static void ListAllHosts(byte[] ipBase, Action<AddressStruct> itemCallback)
{
for (int b4 = 0; b4 <= 255; b4++)
{
var ip = ipBase[0] + "." + ipBase[1] + "." + ipBase[2] + "." + b4;
var ping = new Ping();
ping.PingCompleted += (o, e) =>
{
if (e.Error == null && e.Reply.Status == IPStatus.Success)
if (itemCallback != null)
{
GetMacAddress(
e.Reply.Address,
(mac) =>
{
itemCallback(new AddressStruct()
{
IPAddress = e.Reply.Address,
MacAddress = mac
});
});
}
};
ping.SendAsync(ip, null);
}
}
示例2: Trace
public static void Trace(IPAddress destination, Func<IPAddress, TracertNode, bool> callback)
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
else
{
if (callback == null)
{
throw new ArgumentNullException("callback");
}
else
{
var syncroot = new object();
var buffer = new byte[INT_BufferSize];
var options = new PingOptions(1, true);
var ping = new Ping();
ping.PingCompleted += (sender, e) =>
{
var address = e.Reply.Address;
var status = e.Reply.Status;
back:
var done = !callback.Invoke(destination, new TracertNode(address, status, e.Reply.Options.Ttl)) || address.Equals(destination);
if (done)
{
try
{
if (ping != null)
{
ping.Dispose();
}
}
finally
{
ping = null;
}
}
else
{
lock (syncroot)
{
if (ping == null)
{
address = destination;
status = IPStatus.Unknown;
goto back;
}
else
{
options.Ttl++;
ping.SendAsync(destination, INT_TimeoutMilliseconds, buffer, options, null);
}
}
}
};
ping.SendAsync(destination, INT_TimeoutMilliseconds, buffer, options, null);
}
}
}
示例3: PingAsync
public Task<PingReply> PingAsync(string address)
{
var tcs = new TaskCompletionSource<PingReply>();
t = new Thread(new ThreadStart(() =>
{
Ping ping = new Ping();
ping.PingCompleted += (obj, sender) =>
{
tcs.SetResult(sender.Reply);
};
ping.SendAsync(address, new object());
if (tcs.Task.Result.Status.Equals(IPStatus.Success) || tcs.Task.Result.Status.Equals(IPStatus.TimedOut))
{
isPinged = true;
}
}
));
t.Start();
return tcs.Task;
}
示例4: searchMethod
/// <summary>
/// 线程创建时调用此方法来搜索局域网主机
/// </summary>
private void searchMethod()
{
lock (obj)
{
//因为是刷新列表,所以要先清空
if (result != null)
{
result.Clear();
}
try
{
//扫描整个网段
for (int i = 1; i < 256; i++)
{
//ping主机从而获取其IP地址和主机名
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
//如果路由器不是这么设置的这个地方就要改~~~偷个懒~^_^
string pingIP = "10.0.2." + i.ToString();
//异步ping,防止主界面过长时间不响应
myPing.SendAsync(pingIP, null);
Thread.Sleep(1);
}
}
catch (Exception exp)
{
MessageBox.Show("错误!");
}
}
}
示例5: Submit
/// <summary>
/// Notifies the hub about the updatel
/// </summary>
public void Submit()
{
var ping = new Ping();
string hostName = Hub.AbsoluteUri;
string payload = Topic.AbsoluteUri;
ping.SendAsync(hostName, TimeOut, payload.ToCharArray());
}
示例6: PingAsync
static Task<PingReply> PingAsync(string address)
{
var tcs = new TaskCompletionSource<PingReply>();
Ping ping = new Ping();
ping.PingCompleted += (obj, sender) => {
tcs.SetResult(sender.Reply);
};
ping.SendAsync(address, new object());
return tcs.Task;
}
示例7: Connect
public void Connect()
{
// We first try to ping twitch.tv to make sure it's reachable
string data = "abcdefghijklmopqrstuvwxyz012345";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 5000;
Ping ping = new Ping();
ping.PingCompleted += HandleConnectionPingCompleted;
PingOptions pingOption = new PingOptions(64, true);
ping.SendAsync(Hostname, timeout, buffer, pingOption);
}
示例8: Ping
public void Ping(Action completedAction)
{
Ping ping = new Ping();
PingOptions pingOptions = new PingOptions();
string data = "rotate";
byte[] buffer = Encoding.ASCII.GetBytes(data);
ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);
ping.SendAsync(this.Host, 500, buffer, pingOptions, completedAction);
}
示例9: Execute
public void Execute()
{
//
// 同期での送信.
//
var hostName = "localhost";
var timeOut = 3000;
var p = new Ping();
var r = p.Send(hostName, timeOut);
if (r.Status == IPStatus.Success)
{
Output.WriteLine("Ping.Send() Success.");
}
else
{
Output.WriteLine("Ping.Send() Failed.");
}
//
// 非同期での送信.
//
hostName = "www.google.com";
object userToken = null;
p.PingCompleted += (s, e) =>
{
if (e.Cancelled)
{
Output.WriteLine("Cancelled..");
return;
}
if (e.Error != null)
{
Output.WriteLine(e.Error.ToString());
return;
}
if (e.Reply.Status != IPStatus.Success)
{
Output.WriteLine("Ping.SendAsync() Failed");
return;
}
Output.WriteLine("Ping.SendAsync() Success.");
};
p.SendAsync(hostName, timeOut, userToken);
Thread.Sleep(3000);
}
示例10: ping_Async
public void ping_Async(String sHostNameOrAddressToPing)
{
try
{
var pPing = new System.Net.NetworkInformation.Ping();
pPing.PingCompleted += pPing_PingCompleted;
pPing.SendAsync(sHostNameOrAddressToPing, sHostNameOrAddressToPing);
}
catch (Exception ex)
{
DI.log.error("in Ping: {0}", ex.Message);
}
}
示例11: MyPing
public string MyPing()
{
string ip1 = getIP();
for(int i = 0; i < 255; i++)
{
Ping myPing=new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
// gethost ip
string pingIP =ip1 + i.ToString();
myPing.SendAsync(pingIP, 1000, null);
}
return pingstr;
}
示例12: ScanIPs
public static void ScanIPs()
{
//string hostName = Dns.GetHostName(); // Retrive the Name of HOST
//Console.WriteLine(hostName);
// Get the IP
//string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
string myIP = "192.168.1.1";
var bytes = IPAddress.Parse(myIP).GetAddressBytes();
// set the value here
bytes[3] = 0;
IPAddress ipAddress = new IPAddress(bytes);
var IPaddress1 = IPAddress.Parse(myIP).GetAddressBytes()[0].ToString() + "." + IPAddress.Parse(myIP).GetAddressBytes()[1].ToString() + "." + IPAddress.Parse(myIP).GetAddressBytes()[2].ToString() + ".";
Console.WriteLine("My IP Address is :" + myIP);
Console.WriteLine("Network :" + IPaddress1);
//Console.ReadLine();
countdown = new CountdownEvent(1);
Stopwatch sw = new Stopwatch();
sw.Start();
//string ipBase = "10.0.0.";
for (int i = 1; i < 255; i++)
{
string ip = IPaddress1 + i.ToString();
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
countdown.AddCount();
p.SendAsync(ip, 100, ip);
}
countdown.Signal();
countdown.Wait();
sw.Stop();
TimeSpan span = new TimeSpan(sw.ElapsedTicks);
//Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount);
//Console.WriteLine("\nPress any key to continue...");
//Console.ReadKey();
}
示例13: PingAsync
static Task<PingReply> PingAsync(string address)
{
var tcs = new TaskCompletionSource<PingReply>();
Ping ping = new Ping();
ping.PingCompleted += (obj, sender) =>
{
tcs.SetResult(sender.Reply);
};
ping.SendAsync(address, 200, new object());
if (tcs.Task.Status.ToString() != "Success")
{
fail_count++;
}
return tcs.Task;
}
示例14: RefreshServerList
public static void RefreshServerList()
{
IPAddress ipAddress = Array.FindAll(Dns.GetHostAddresses(Dns.GetHostName()), a => a.AddressFamily == AddressFamily.InterNetwork).First();
string[] baseIP = ipAddress.ToString().Split('.');
for (int j = Int32.Parse(baseIP[2]); j < Int32.Parse(baseIP[2]) + 5; j++) {
for (int i = 0; i < 255; i++) {
try {
IPAddress ip = IPAddress.Parse(baseIP[0] + "." + baseIP[1] + "." + j + "." + i);
Ping ping = new Ping();
ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
ping.SendAsync(ip, 600, ip);
} catch (Exception) { }
}
}
}
示例15: btnDomain2IP_Click
private void btnDomain2IP_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
try
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
string hostName = dataGridView1[0, i].Value.ToString();
myPing.SendAsync(hostName, 1, i);
}
catch
{
}
}
}