C
C#6mo ago
Suzz

network upload and download speed using System.Net.NetworkInformation package in .net core

the below is my code i've tried using System.Net.NetworkInformation libraray. using System.Net.NetworkInformation; var interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (var networkInterface in interfaces) if (networkInterface.OperationalStatus == OperationalStatus.Up && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Unknown && !networkInterface.IsReceiveOnly) { Console.WriteLine($"Monitoring network speed for interface: {networkInterface.Name}"); var bytesSent = networkInterface.GetIPv4Statistics().BytesSent; var bytesReceived = networkInterface.GetIPv4Statistics().BytesReceived; long uploadSpeed = CalculateSpeed(bytesSent); long downloadSpeed = CalculateSpeed(bytesReceived); Console.WriteLine($"Bytes Sent: {bytesSent} bytes | Bytes Received: {bytesReceived} bytes"); Console.WriteLine($"Upload Speed: {uploadSpeed} Mbps | Download Speed: {downloadSpeed} Mbps"); } Console.ReadKey(); static long CalculateSpeed(long bytes) { const double bytesToMegabits = 8.0 / (1000 * 1000); return (long)(bytes * bytesToMegabits); } output is: Monitoring network speed for interface: Wi-Fi Bytes Sent: 46381565 bytes | Bytes Received: 1586958802 bytes Upload Speed: 371 Mbps | Download Speed: 12695 Mbps
No description
8 Replies
cap5lut
cap5lut6mo ago
u are calculating the speed wrong, BytesReceived and BytesSent is the total amount of bytes it received/sent since the network connection was established. to calculate the speed u would have to get the numbers, wait some seconds, get the numbers again and from the difference/seconds u waited u get the bytes/sec for that time (assuming that ur question is why have have such weirdly high speed numbers)
Suzz
Suzz6mo ago
ok are there any methods available in System.Net.NetworkInformation like networkInterface.GetIPv4Statistics().BytesSent; networkInterface.GetIPv4Statistics().BytesReceived; my requirement is i need to get the upload and download speed on a single button click. are there any ways to do that? the workflow is like that i have a wpf application which have to calculate the upload and downlaod speed of the system. and i have an asp.netcore application, which have a get api. i'm using signalr callback to the wpf application from the asp.net core on the get api called. so there is not much time for me to wait. also my requirement is to use any dotnet packages to retrieve the network speed. i've tried to use the LibreHardwareMonitorpackage. but it needs admin privilege to get the sensor values. the uat should need, thats not possible for my case
cap5lut
cap5lut6mo ago
[..] speed of the system
which system is meant here? the local machine or the server? if its regarding the server then the NetworkInformation will not help much as u can not differentiate between data I/O to the server or other servers but generally speaking there are 2 ways to measure, either u send/receive a specific amount of data and measure how long that takes, or u send/receive as much of data in a specific time frame. the more data/the longer the time frame, the more accurate this becomes
Suzz
Suzz6mo ago
which system is meant here? the local machine or the server? the local machine, where the wpf application is
cap5lut
cap5lut6mo ago
well, u would in either way send some data to some server to get the upload speed/download some data to get the download speed eg if u have some kind of echo endpoint, u could 1) prepare a http post request with a body of 10mb data 2) store how many bytes the interface has sent so far 3) start a stopwatch 4) send the request with HttpCompletionOption.ResponseHeadersRead 5) store how many bytes the interface sent 6) store the elapsed time 7) store the received bytes 8) consume the response body 9) store the received bytes 10) store the elapsed time can also be less than 10mb data, but as said as smaller the time frame as less accurate it is with 2) and 5) u know how much bytes the local machine sent, with 6) u know how long it took to send these bytes with 7) and 9) u know how much bytes the local machine received and with 6) and 10) u know how long it took to receive these bytes
Suzz
Suzz6mo ago
ok, thankyou for the help
cap5lut
cap5lut6mo ago
i dunno much about signalr, but u could probably also use that instead of http requests
Suzz
Suzz6mo ago
thats ok, i wanted to use dontnet package instead of doing pings or socket. the requirement is like that. thats why i tried to use the System.Net.NetworkInformation
Want results from more Discord servers?
Add your server
More Posts