02/10/2018, 00:48

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

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 tên quốc tế là bandawidth . Thuật ngữ này dùng để chỉ ...

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à bandawidth. 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 bandawidth, upload và download bằng C#

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

namespace InterfaceTrafficWatch
{
    /// 

/// Network Interface Traffic Watch /// by Mohamed Mansour /// /// Free to use under GPL open source license! ///

public partial class MainForm : Form { ///

/// Timer Update (every 1 sec) ///

private const double timerUpdate = 1000; ///

/// Interface Storage ///

private NetworkInterface[] nicArr; ///

/// Main Timer Object /// (we could use something more efficient such /// as interop calls to HighPerformanceTimers) ///

private Timer timer; ///

/// Constructor ///

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

/// Initialize all network interfaces on this computer ///

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) { // Grab NetworkInterface object that describes the current interface NetworkInterface nic = nicArr[cmbInterface.SelectedIndex]; // Grab the stats for that interface IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); long bytesSentSpeed = (long)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024; long bytesReceivedSpeed = (long)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024; // Update the labels 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

Tags: download fileupload fileđọc fileghi file
0