在這一篇文章中 WIFI RvO TEST TOOL FROM C# 用到了ping的功能去察看跟DUT_AP的連線情況
圖一: Ping Function
public bool Ping(string nameOrAddress, string timeout)
{
bool pingable = false;
int pingcnt = 0;
try
{
string pingrst;
Process p = new Process();
p.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ping -n 6 " + nameOrAddress + " -w " + timeout);
p.StandardInput.WriteLine("exit");
CommonData.pFindStr.Source = p.StandardOutput.ReadToEnd();
if (CommonData.pFindStr.Source.IndexOf("TTL=") != -1)
{
pingcnt = SubstringCount(CommonData.pFindStr.Source, "TTL=");
if (pingcnt > 2)
{
CommonData.WriteMessage(CommonData.richtextbox, DateTime.Now.ToString() + CommonData.pFindStr.Source, "", Color.Blue, Color.Green);
pingrst = "連接";
CommonData.WriteMessage(CommonData.richtextbox, pingrst, pingcnt.ToString(), Color.Blue, Color.Green);
pingable = true;
}
}
else if (CommonData.pFindStr.Source.IndexOf("Destination host unreachable.") != -1 || CommonData.pFindStr.Source.IndexOf("一般失敗") != -1)
{
//一般失敗
this.GridData.WriteMessage(this.GridData._TPars.richtextbox, DateTime.Now.ToString() + CommonData.pFindStr.Source, "", Color.Blue, Color.Green);
pingrst = "無法到達目的主機";
this.GridData.WriteMessage(this.GridData._TPars.richtextbox, pingrst, "", Color.Blue, Color.Green);
}
else if (CommonData.pFindStr.Source.IndexOf("Request timed out.") != -1 || CommonData.pFindStr.Source.IndexOf("要求等候逾時") != -1)
{
//要求等候逾時
this.GridData.WriteMessage(this.GridData._TPars.richtextbox, DateTime.Now.ToString() + CommonData.pFindStr.Source, "", Color.Blue, Color.Green);
pingrst = "超時";
this.GridData.WriteMessage(this.GridData._TPars.richtextbox, pingrst, "", Color.Blue, Color.Green);
}
else if (CommonData.pFindStr.Source.IndexOf("Unknown host") != -1)
{
this.GridData.WriteMessage(this.GridData._TPars.richtextbox, DateTime.Now.ToString() + CommonData.pFindStr.Source, "", Color.Blue, Color.Green);
pingrst = "無法解析主機";
this.GridData.WriteMessage(this.GridData._TPars.richtextbox, pingrst, "", Color.Blue, Color.Green);
}
p.Close();
}
catch (Exception e)
{
CommonData.WriteMessage(CommonData.richtextbox, e.Message, "", Color.Red, Color.Green);
}
return pingable;
}
沒有留言:
張貼留言