pingIPAddress

来源:互联网 发布:游戏帧数优化器 编辑:程序博客网 时间:2024/06/06 01:53

计算网络延迟

程序下载地址:http://download.csdn.net/detail/jsjyyjs07/5282455

//获取IPIPHostEntry 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);             }        }