計算網絡延遲
程序下載地址:http://download.csdn.net/detail/jsjyyjs07/5282455
//獲取IP
IPHostEntry ipHostInfo = Dns.Resolve(txtAddress.Text);
//IPHostEntry iphost = Dns.GetHostByName(textBox4.Text);
int i = ipHostInfo.AddressList.Length;
//int j = iphost.AddressList.Length;
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress ip = iphost.AddressList[0];
IPAddress ipaddress = ipHostInfo.AddressList[0];
//實現Ping方法
public void Ping()
{
//實現Ping
string ipaddress = cbIP.Text;
if (txtNumber.Text != "" && cbIP.Text != "")
{
cbIP.Enabled = false;
}
if (cbIP.Text == "")
{
tsslIP.Text = " ";
}
else
{
tsslIP.Text = ipaddress;
}
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//Wait seconds for a reply.
int timeout = 3000;
int time1 = 3000;
int time2 = 0;
try
{
if (cbIP.Text != "" && txtNumber.Text != "")
{
int l, k, z, i1;
string s1, s2, s3, s4, s5;
int m = 0;
int o = 0;
int x = 0;
float y;
float n = int.Parse(txtNumber.Text);
k = 0;
l = 0;
y = 0;
z = 0;
ltbShow.Items.Add("正在Ping " + txtAddress.Text + " [" + ipaddress + "] " + "具有 " + "32" + " 字節的數據");
tsslIP.Text = ipaddress.ToString();
txtNumber.Enabled = false;
btnPing.Enabled = false;
Thread.Sleep(100);
for (k = 1; k <= n; k++) { Thread.Sleep(1000); System.Net.NetworkInformation.PingReply reply = p.Send(ipaddress, timeout, buffer, options); s5 = reply.Status.ToString(); i1 = int.Parse(reply.RoundtripTime.ToString()); if (s5 == "Success") { s1 = reply.Address.ToString(); s2 = reply.RoundtripTime.ToString(); s3 = reply.Options.Ttl.ToString(); s4 = reply.Buffer.Length.ToString();
if (i1 == 0) { ltbShow.Items.Add("來自 " + s1 + " 的回覆:字節=" + s4 + " 時間<1ms TTL=" + s3); } else { ltbShow.Items.Add("來自 " + s1 + " 的回覆:字節=" + s4 + " 時間=" + s2 + "ms TTL=" + s3); } if (time1 > i1) { time1 = i1; } if (time2 < i1) { time2 = i1; } l++; m = i1 + m; tsslYC.Text = (m / l).ToString() + "ms"; label2.Text = "延遲:"+ s2.ToString() + "ms"; //listbox1追蹤顯示下一項 SendMessage(ltbShow.Handle, WM_VSCROLL, SB_LINEDOWN, 0); } else { ltbShow.Items.Add("請求超時。"); x++; y++; } if (k == n && y != n) { o = m / l; z = (int)(y / n * 100); ltbShow.Items.Add(ipaddress + " 的Ping統計信息:"); ltbShow.Items.Add(" 數據包:已發送 = " + txtNumber.Text + ", 已接收 = " + l + ", 丟失 = " + x + " (" + z + "% 丟失)"); ltbShow.Items.Add("往返行程的估計時間(以毫秒爲單位):"); ltbShow.Items.Add(" 最短 = " + time1 + "ms 最長 = " + time2 + "ms 平均 = " + o + "ms"); ltbShow.Items.Add(" ");
//toolStripStatusLabel4.Text = z.ToString() + "%"; tsslYC.Text = o.ToString() + "ms"; label2.Text = "延遲:阻塞"; } else if (k == n && y == n) { //z = (int)(y / n * 100); ltbShow.Items.Add(ipaddress + " 的Ping統計信息:"); ltbShow.Items.Add(" 數據包:已發送 = " + txtNumber.Text + ", 已接收 = " + l + ", 丟失 = " + x + " (" + z + "% 丟失)"); ltbShow.Items.Add(" ");
//toolStripStatusLabel4.Text = z.ToString() + "%"; tsslYC.Text = "NoPing"; label2.Text = "延遲:阻塞"; } if (x == k) { tsslYC.Text = "NoPing"; label2.Text = "延遲:阻塞"; } z = (int)(y / k * 100); tsslDBL.Text = z.ToString() + "%"; //listbox1追蹤顯示最後一項 SendMessage(ltbShow.Handle, WM_VSCROLL, SB_BOTTOM, 0); } txtAddress.Enabled = true; btnGetIP.Enabled = true; cbIP.Enabled = true; cbIP.Items.Clear(); label2.Text = ""; } } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }