欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 测试网络速度例子

程序员文章站 2022-06-21 09:42:07
using System.Net.NetworkInformation; namespace PingExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } priva... ......
using system.net.networkinformation;  
namespace pingexample
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }

        private void btn_startping_click(object sender, eventargs e)
        {
            this.lst_pingresult.items.clear();
            //远程服务器ip
            string ipstr = txt_ipaddress.text.tostring().trim();
            //构造ping实例
            ping pingsender = new ping();
            //ping 选项设置
            pingoptions options = new pingoptions();
            options.dontfragment = true;
            //测试数据
            string data = "test data abcabc";
            byte[] buffer = encoding.ascii.getbytes(data);
            //设置超时时间
            int timeout = 120;
            //调用同步 send 方法发送数据,将返回结果保存至pingreply实例
            pingreply reply = pingsender.send(ipstr, timeout, buffer, options);
            if (reply.status == ipstatus.success)
            {
                lst_pingresult.items.add("答复的主机地址:" + reply.address.tostring());
                lst_pingresult.items.add("往返时间:" + reply.roundtriptime);
                lst_pingresult.items.add("生存时间(ttl):" + reply.options.ttl);
                lst_pingresult.items.add("是否控制数据包的分段:" + reply.options.dontfragment);
                lst_pingresult.items.add("缓冲区大小:" + reply.buffer.length);
            }
            else
                lst_pingresult.items.add(reply.status.tostring());
        }
    }
}
https://www.cnblogs.com/daming1233/p/6598028.html