本文整理汇总了C#中Ping.Send方法的典型用法代码示例。如果您正苦于以下问题:C# Ping.Send方法的具体用法?C# Ping.Send怎么用?C# Ping.Send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ping
的用法示例。
在下文中一共展示了Ping.Send方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: checkConnection
// The state object is necessary for a TimerCallback.
public void checkConnection(object stateObject)
{
Process p = new Process();
Ping pingSender = new Ping ();
p.StartInfo.FileName = "arp";
p.StartInfo.Arguments = "-a";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes (data);
for(int i = 0; i < 25 ; i++){
pingSender.Send ("10.0.0."+i.ToString(),10,buffer);
}
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
string MAC = "xx-xx-xx-xx-xx-xx";
if(output.Contains(MAC)){
SerialPort port = new SerialPort("COM5", 9600);
port.Open();
port.Write("u");
port.Close();
}
else{
SerialPort port = new SerialPort("COM5", 9600);
port.Open();
port.Write("l");
port.Close();
}
}
示例2: Main
public static void Main(string [] args)
{
int pingsc = 0, pingf = 0;
int numb = Convert.ToInt32(args[1]);
Ping ping = new Ping();
PingOptions option = new PingOptions();
option.DontFragment = true;
string message = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasdfasdfasdfasfasdfasdfasdfasdfasdfas";
byte [] val = Encoding.ASCII.GetBytes(message);
int timeout = 120;
for(int i = 0; i < numb; i++)
{
PingReply reply = ping.Send(args[0], timeout, val, option);
if(reply.Status == IPStatus.Success)
{
Console.WriteLine("Address {0} " , reply.Address.ToString());
Console.WriteLine("RoundTrip time {0} " ,reply.RoundtripTime);
Console.WriteLine("Time to live {0}", reply.Options.Ttl);
Console.WriteLine("Buffer size {0}", reply.Buffer.Length);
pingsc++;
}
else
{
Console.WriteLine("Ping failed");
pingf++;
}
}
Console.WriteLine("number of Success pings {0}", pingsc);
Console.WriteLine("Number of failed pings {0}", pingf);
}
示例3: Main
public static void Main(string[] args)
{
using (Ping ping = new Ping())
{
try
{
PingReply reply = ping.Send("127.0.0.1", 100);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Success - IP Address:{0} Time:{1}ms",
reply.Address, reply.RoundtripTime);
}
else
{
Console.WriteLine(reply.Status);
}
}
catch (Exception ex)
{
Console.WriteLine("Error ({0})", ex.InnerException.Message);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
示例4: SendPingAsync_InvalidArgs
public async Task SendPingAsync_InvalidArgs()
{
IPAddress localIpAddress = await TestSettings.GetLocalIPAddress();
Ping p = new Ping();
// Null address
Assert.Throws<ArgumentNullException>("address", () => { p.SendPingAsync((IPAddress)null); });
Assert.Throws<ArgumentNullException>("hostNameOrAddress", () => { p.SendPingAsync((string)null); });
Assert.Throws<ArgumentNullException>("address", () => { p.SendAsync((IPAddress)null, null); });
Assert.Throws<ArgumentNullException>("hostNameOrAddress", () => { p.SendAsync((string)null, null); });
Assert.Throws<ArgumentNullException>("address", () => { p.Send((IPAddress)null); });
Assert.Throws<ArgumentNullException>("hostNameOrAddress", () => { p.Send((string)null); });
// Invalid address
Assert.Throws<ArgumentException>("address", () => { p.SendPingAsync(IPAddress.Any); });
Assert.Throws<ArgumentException>("address", () => { p.SendPingAsync(IPAddress.IPv6Any); });
Assert.Throws<ArgumentException>("address", () => { p.SendAsync(IPAddress.Any, null); });
Assert.Throws<ArgumentException>("address", () => { p.SendAsync(IPAddress.IPv6Any, null); });
Assert.Throws<ArgumentException>("address", () => { p.Send(IPAddress.Any); });
Assert.Throws<ArgumentException>("address", () => { p.Send(IPAddress.IPv6Any); });
// Negative timeout
Assert.Throws<ArgumentOutOfRangeException>("timeout", () => { p.SendPingAsync(localIpAddress, -1); });
Assert.Throws<ArgumentOutOfRangeException>("timeout", () => { p.SendPingAsync(TestSettings.LocalHost, -1); });
Assert.Throws<ArgumentOutOfRangeException>("timeout", () => { p.SendAsync(localIpAddress, -1, null); });
Assert.Throws<ArgumentOutOfRangeException>("timeout", () => { p.SendAsync(TestSettings.LocalHost, -1, null); });
Assert.Throws<ArgumentOutOfRangeException>("timeout", () => { p.Send(localIpAddress, -1); });
Assert.Throws<ArgumentOutOfRangeException>("timeout", () => { p.Send(TestSettings.LocalHost, -1); });
// Null byte[]
Assert.Throws<ArgumentNullException>("buffer", () => { p.SendPingAsync(localIpAddress, 0, null); });
Assert.Throws<ArgumentNullException>("buffer", () => { p.SendPingAsync(TestSettings.LocalHost, 0, null); });
Assert.Throws<ArgumentNullException>("buffer", () => { p.SendAsync(localIpAddress, 0, null, null); });
Assert.Throws<ArgumentNullException>("buffer", () => { p.SendAsync(TestSettings.LocalHost, 0, null, null); });
Assert.Throws<ArgumentNullException>("buffer", () => { p.Send(localIpAddress, 0, null); });
Assert.Throws<ArgumentNullException>("buffer", () => { p.Send(TestSettings.LocalHost, 0, null); });
// Too large byte[]
Assert.Throws<ArgumentException>("buffer", () => { p.SendPingAsync(localIpAddress, 1, new byte[65501]); });
Assert.Throws<ArgumentException>("buffer", () => { p.SendPingAsync(TestSettings.LocalHost, 1, new byte[65501]); });
Assert.Throws<ArgumentException>("buffer", () => { p.SendAsync(localIpAddress, 1, new byte[65501], null); });
Assert.Throws<ArgumentException>("buffer", () => { p.SendAsync(TestSettings.LocalHost, 1, new byte[65501], null); });
Assert.Throws<ArgumentException>("buffer", () => { p.Send(localIpAddress, 1, new byte[65501]); });
Assert.Throws<ArgumentException>("buffer", () => { p.Send(TestSettings.LocalHost, 1, new byte[65501]); });
}
示例5: Status
public static String Status(String IP)
{
IPAddress ip = IPAddress.Parse(IP);
try
{
Ping ping = new Ping();
PingReply reply = ping.Send(ip);
return Convert.ToString(reply.Status);
}
catch(Exception)
{
return "Error";
}
}
示例6: Main
static void Main (string [] args)
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte [] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args [0], timeout, buffer, options);
Console.WriteLine ("Status: {0}", reply.Status);
}
示例7: CheckInternetConnection
public bool CheckInternetConnection(String ipAddress)
{
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(ipAddress, 1000);
if (reply.Status == IPStatus.Success)
{
result = true;
}
}
catch (PingException)
{
result = false;
}
return result;
}
示例8: Connect
public static ICSharpRpcClient Connect(string ipaddress)
{
Ping pinger = new Ping();
PingReply pr = pinger.Send(ipaddress);
if (pr.Status == IPStatus.Success)
{
Console.WriteLine("Connect to {0}", ipaddress);
newProxy = XmlRpcProxyGen.Create<ICSharpRpcClient>();
string newProxyUrl = ServerUrlStart + ipaddress + ServerUrlEnd;
newProxy.Url = newProxyUrl;
Console.WriteLine("XML-RPC Client call to : http://" + ipaddress + ":1090/xmlrpc/xmlrpc");
return newProxy;
}
else
{
return null;
}
}
示例9: CheckHostAlive
private void CheckHostAlive()
{
Ping pingSender = new Ping();
PingReply pReply;
try
{
pReply = pingSender.Send(HostName);
if (pReply.Status == IPStatus.Success)
{
if ((string)this.btnHostStatus.Tag == "checking")
{
ShowStatusImage(global::My.Resources.Resources.HostStatus_On);
}
}
else
{
if ((string)this.btnHostStatus.Tag == "checking")
{
ShowStatusImage(global::My.Resources.Resources.HostStatus_Off);
}
}
}
catch (Exception)
{
if ((string)this.btnHostStatus.Tag == "checking")
{
ShowStatusImage(global::My.Resources.Resources.HostStatus_Off);
}
}
}
示例10: Sends_ReuseInstance_Hostname
public static async Task Sends_ReuseInstance_Hostname()
{
IPAddress localIpAddress = await TestSettings.GetLocalIPAddress();
using (Ping p = new Ping())
{
for (int i = 0; i < 3; i++)
{
PingReply pingReply = p.Send(TestSettings.LocalHost);
Assert.Equal(IPStatus.Success, pingReply.Status);
Assert.True(pingReply.Address.Equals(localIpAddress));
}
}
}
示例11: triggerButton_Click
protected void triggerButton_Click(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
LabelInfo.Text = "正在处理中,请稍等。。。";
triggerButton.Enabled = false;
//服务器IP
List<ServerInfo> list = new List<ServerInfo>();
ServerInfo info1 = new ServerInfo();
info1.Port = 0xca4d;
info1.Ip = "112.132.215.30";
list.Add(info1);
ServerInfo info2 = new ServerInfo();
info2.Port = 0x6bb0;
info2.Ip = "112.213.122.141";
list.Add(info2);
ServerInfo info3 = new ServerInfo();
info3.Port = 0xdba6;
info3.Ip = "117.25.147.236";
list.Add(info3);
ServerInfo info4 = new ServerInfo();
info4.Port = 0xd1c3;
info4.Ip = "112.132.212.59";
list.Add(info4);
ServerInfo info5 = new ServerInfo();
info5.Port = 0x4ec0;
info5.Ip = "206.161.218.27";
list.Add(info5);
ServerInfo info6 = new ServerInfo();
info6.Port = 0xccb1;
info6.Ip = "211.55.29.30";
list.Add(info6);
string curIp = "117.25.147.236";
int curPort = 0xdba6;
Ping curP = new Ping();
if (curP.Send(curIp).Status != IPStatus.Success)
{
foreach (ServerInfo si in list){
Ping p = new Ping();
if (p.Send(si.Ip).Status == IPStatus.Success)
{
curIp = si.Ip;
curPort = si.Port;
}
}
if (curIp == "117.25.147.236")
{
LabelInfo.Text = "网络不通,连接服务器失败";
triggerButton.Enabled = true;
return;
}
}
short UserType = 0;
string errMsg = string.Empty;
if (RadioButton1.Checked)
{
UserType = 1;
}
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//EndPoint point = new IPEndPoint(IPAddress.Parse("117.25.147.236"), 0xdba6);
EndPoint point = new IPEndPoint(IPAddress.Parse(curIp), curPort);
s.Connect(point);
if (!s.Connected)
{
// Connection failed, try next IPaddress.
s = null;
LabelInfo.Text = "网络不通,连接服务器失败";
triggerButton.Enabled = true;
return;
}
else
{
/********************登陆********************/
LoginInfo li = new LoginInfo();
li.UserName = ConfigurationManager.AppSettings["user"];
li.Password = Encrypt.sEncrypt(ConfigurationManager.AppSettings["pwd"]);
li.Ver = "2.0.1";
//Send(0x80001, li.GetBytes())
byte[] datal = li.GetBytes();
int sizel = datal.Length;
int offsetl = 0;
int msgl = 0x80001;
byte[] destinationArrayl = new byte[sizel + 8];
Array.Copy(BitConverter.GetBytes(msgl), 0, destinationArrayl, 0, 4);
Array.Copy(BitConverter.GetBytes(sizel), 0, destinationArrayl, 4, 4);
if ((datal != null) && (datal.Length >= (offsetl + sizel)))
{
Array.Copy(datal, offsetl, destinationArrayl, 8, sizel);
}
try
{
s.Send(destinationArrayl, destinationArrayl.Length, 0);//发包
byte[] buf = new byte[1024];
//.........这里部分代码省略.........
示例12: btnVirusScan_Click
protected void btnVirusScan_Click(object sender, System.EventArgs e)
{
clearConsole();
// DECLARE OBJECTS AND VARIABLES
Thread clamavThread = new Thread(clamScanThread);
Thread clamavInstallationThread = new Thread(clamavInstallThread);
// ADDRESS REPRESENTS UBUNTU UPDATE MIRROR, FAILING TO CONNECT
// WILL HALT THE UPDATE PROCESS TO PROTECT THE SYSTEM
string host = "archive.ubuntu.com";
Ping p = new Ping();
if (File.Exists(CLAMSCAN_BIN))
{
// CLAMAV IS INSTALLED, BEGIN VIRUS SCAN
clamavThread.Start();
progressBarPulse();
}
else
{
// CLAMAV IS NOT INSTALLED
// DETECT NETWORK CONNECTION BEFORE ATTEMPTING INSTALLATION
try
{
PingReply reply = p.Send(host, 1000);
if (reply.Status == IPStatus.Success)
{
// INTERNET CONENCTION EXISTS
// INSTALL SOFTWARE
clamavInstallationThread.Start();
progressBarPulse();
}
}
catch
{
// INTERNET CONNECTION DOES NOT EXIST
MessageDialog mBoxError = new MessageDialog (this,
DialogFlags.DestroyWithParent,
MessageType.Error,
ButtonsType.Close,
"ClamAV is not installed. An active Internet connection was not detected. Please connect to the Internet to install ClamAV.");
mBoxError.Run();
mBoxError.Destroy();
}
}
}
示例13: btnRefreshPackageCatalog_Click
protected void btnRefreshPackageCatalog_Click(object sender, System.EventArgs e)
{
clearConsole();
// DECLARE OBJECTS AND VARIABLES
Thread aptGetUpdateThread = new Thread(updateThread);
// ADDRESS REPRESENTS UBUNTU UPDATE MIRROR, FAILING TO CONNECT
// WILL HALT THE UPDATE PROCESS TO PROTECT THE SYSTEM
string host = "archive.ubuntu.com";
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 1000);
if (reply.Status == IPStatus.Success)
{
// INTERNET CONENCTION EXISTS
// INSTALL SOFTWARE
aptGetUpdateThread.Start();
progressBarPulse();
}
}
catch
{
// DISPLAY ERROR WINDOW
noInternetConnection();
}
}
示例14: btnFreshclam_Click
protected void btnFreshclam_Click(object sender, System.EventArgs e)
{
clearConsole();
// UPDATE VIRUS DEFINITIONS
Thread updateClamavThread = new Thread(freshclamThread);
// ADDRESS REPRESENTS CLAMAV UPDATE MIRROR, FAILING TO CONNECT
// WILL HALT THE UPDATE PROCESS TO PROTECT THE SYSTEM
string host = "clamav.net";
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 1000);
if (reply.Status == IPStatus.Success)
{
// INTERNET CONNECTION EXISTS
updateClamavThread.Start();
// BEGIN PULSING THE PROGRESS BAR
progressBarPulse();
}
}
catch
{
// DISPLAY ERROR WINDOW
noInternetConnection();
}
}