• C# Ping的例子,可用于测试网络,延迟xx毫秒 C#编写网站测速


    C#编写网站测速 

    WebClient wcl = new WebClient();
    Stopwatch spwatch = new Stopwatch();
    spwatch.Start();
    byte[] resultBytes = wcl.DownloadData(new Uri(textBox1.Text));
    spwatch.Stop();
    MessageBox.Show(spwatch.Elapsed.ToString());
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.NetworkInformation;
    
    namespace PingExample
    {
        public partial class Form1 : Form
        {
            #region 设计
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.txt_IPAddress = new System.Windows.Forms.TextBox();
                this.lst_PingResult = new System.Windows.Forms.ListBox();
                this.btn_StartPing = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(30, 24);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(77, 12);
                this.label1.TabIndex = 0;
                this.label1.Text = "远程主机IP:";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(30, 61);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(65, 12);
                this.label2.TabIndex = 1;
                this.label2.Text = "测试结果:";
                // 
                // txt_IPAddress
                // 
                this.txt_IPAddress.Location = new System.Drawing.Point(113, 21);
                this.txt_IPAddress.Name = "txt_IPAddress";
                this.txt_IPAddress.Size = new System.Drawing.Size(148, 21);
                this.txt_IPAddress.TabIndex = 2;
                // 
                // lst_PingResult
                // 
                this.lst_PingResult.FormattingEnabled = true;
                this.lst_PingResult.ItemHeight = 12;
                this.lst_PingResult.Location = new System.Drawing.Point(32, 87);
                this.lst_PingResult.Name = "lst_PingResult";
                this.lst_PingResult.Size = new System.Drawing.Size(229, 136);
                this.lst_PingResult.TabIndex = 3;
                // 
                // btn_StartPing
                // 
                this.btn_StartPing.Location = new System.Drawing.Point(105, 239);
                this.btn_StartPing.Name = "btn_StartPing";
                this.btn_StartPing.Size = new System.Drawing.Size(75, 23);
                this.btn_StartPing.TabIndex = 4;
                this.btn_StartPing.Text = "Ping";
                this.btn_StartPing.UseVisualStyleBackColor = true;
                this.btn_StartPing.Click += new System.EventHandler(this.btn_StartPing_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 284);
                this.Controls.Add(this.btn_StartPing);
                this.Controls.Add(this.lst_PingResult);
                this.Controls.Add(this.txt_IPAddress);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Name = "Form1";
                this.Text = "ping 类的用法";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox txt_IPAddress;
            private System.Windows.Forms.ListBox lst_PingResult;
            private System.Windows.Forms.Button btn_StartPing;
            #endregion
    
    
            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 = "";
                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());
            }
        }
    }
  • 相关阅读:
    知社 —— 第 4 次站立式会议(04-28)
    知社 —— 第 3 次站立式会议(04-27)
    知社 —— 第 2 次站立式会议(04-26)
    知社 —— 第 1 次站立式会议(04-25)
    团队代码规范、冲刺任务与计划
    团队作业第四次 — 项目系统设计与数据库设计
    团队作业第三次 — 项目需求分析
    第01组 Alpha冲刺(5/6)
    2019 SDN上机第4次作业
    2019 SDN阅读作业
  • 原文地址:https://www.cnblogs.com/chengulv/p/4520562.html
Copyright © 2020-2023  润新知