NEWS

[C#] Đo băng thông, bandwidth, download, ip và upload traffic

[C#] Đo băng thông, bandwidth, download, ip và upload traffic
Đăng bởi: Thảo Meo - Lượt xem: 11138 12:13:39, 24/04/2017C#   In bài viết

Bài viết hôm nay, mình xin hướng dẫn các bạn code C#, cách xem băng thông và tốc độ download và upload của card internet bằng ngôn ngữ C#.

Băng thông (BandWidth) là gì?

băng thông c#

Băng thông tên quốc tế là bandwidth. Thuật ngữ này dùng để chỉ lưu lượng của tín hiệu điện được truyền qua thiết bị truyền dẫn trong một giây là bao nhiêu. 

Trong lĩnh vực lưu trữ website, thuật ngữ "băng thông" thường được sử dụng để mô tả số lượng dữ liệu tối đa, mà bạn được phép trao đổi (bao gồm upload và download) qua lại giữa website (hoặc server) và người sử dụng trong một đơn vị thời gian (thường là tháng). Tóm lại, băng thông là thông số chỉ dung lượng tối đa mà website của bạn được lưu chuyển qua lại mỗi tháng.

Giao diện demo ứng dụng:

đo tốc độ upload và download c#

Souce code bandwidth, upload và download bằng C#

using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Collections.Generic;

namespace InterfaceTrafficWatch {


 public partial class MainForm: Form {


  private
  const double timerUpdate = 1000;


  private NetworkInterface[] nicArr;



  private Timer timer;



  public MainForm() {
   InitializeComponent();
  }
  private void MainForm_Load(object sender, EventArgs e) {
   InitializeTimer();
  }



  private void InitializeNetworkInterface() {
   nicArr = NetworkInterface.GetAllNetworkInterfaces();
   List goodAdapters = new List();
   foreach(NetworkInterface nicnac in nicArr) {
    if (nicnac.SupportsMulticast && nicnac.GetIPv4Statistics().UnicastPacketsReceived >= 1 && nicnac.OperationalStatus.ToString() == "Up") {
     goodAdapters.Add(nicnac.Name);
     cmbInterface.Items.Add(nicnac.Name);
    }
   }
   if (goodAdapters.Count != cmbInterface.Items.Count && goodAdapters.Count != 0) {
    cmbInterface.Items.Clear();
    foreach(string gadpt in goodAdapters) {
     cmbInterface.Items.Add(gadpt);
    }
    cmbInterface.SelectedIndex = 0;
   }
   if (goodAdapters.Count == 0) cmbInterface.Items.Clear();
  }
  Initialize the Timer private void InitializeTimer() {
   timer = new Timer();
   timer.Interval = (int) timerUpdate;
   timer.Tick += new EventHandler(timer_Tick);
   timer.Start();
  }
  Update GUI components
  for the network interfaces private void UpdateNetworkInterface() {
   MessageBox.Show(cmbInterface.Items.Count.ToString());
   if (cmbInterface.Items.Count >= 1) {
    interface NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];
    IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();
    long bytesSentSpeed = (long)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
    long bytesReceivedSpeed = (long)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;
    lblSpeed.Text = nic.Speed.ToString();
    lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
    lblSpeed.Text = (nic.Speed).ToString("N0");
    lblBytesReceived.Text = interfaceStats.BytesReceived.ToString("N0");
    lblBytesSent.Text = interfaceStats.BytesSent.ToString("N0");
    lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
    lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";
    UnicastIPAddressInformationCollection ipInfo = nic.GetIPProperties().UnicastAddresses;
    foreach(UnicastIPAddressInformation item in ipInfo) {
     if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
      labelIPAddress.Text = item.Address.ToString();
      uniCastIPInfo = item;
      break;
     }
    }
   }
  }
  void timer_Tick(object sender, EventArgs e) {
   InitializeNetworkInterface();
   UpdateNetworkInterface();
  }
 }
}

Các bạn có thể download code ở bên dưới để tham khảo.

 

Have fun :)

 

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Đo băng thông, bandwidth, download, ip và upload traffic
Đăng bởi: Thảo Meo - Lượt xem: 11138 12:13:39, 24/04/2017C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.