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());
        }
    }
}
相關文章
相關標籤/搜索