Internet速度和带宽使用情况

时间:2013-05-11 15:10:48

标签: c# network-programming network-interface

问题在于:我需要编写一个程序,它将测量我的网络速度以及我的计算机当前使用的程度,并将其放入带有两个系列的折线图中。在谷歌和stackoverflow上的帖子的指导下,我结束了这段代码:

private long bytesReceivedPrev = 0;
    private void CheckBandwidthUsage(DateTime now)
    {
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        long bytesReceived = 0;
        foreach (NetworkInterface inf in interfaces)
        {
            if (inf.OperationalStatus == OperationalStatus.Up &&
                inf.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                inf.NetworkInterfaceType != NetworkInterfaceType.Tunnel &&
                inf.NetworkInterfaceType != NetworkInterfaceType.Unknown && !inf.IsReceiveOnly)
            {
                bytesReceived += inf.GetIPv4Statistics().BytesReceived;
            }
        }

        if (bytesReceivedPrev == 0)
        {
            bytesReceivedPrev = bytesReceived;
        }
        long bytesUsed = bytesReceived - bytesReceivedPrev;
        double kBytesUsed = bytesUsed / 1024;
        double mBytesUsed = kBytesUsed / 1024;
        internetUsage.Add(now, mBytesUsed);
        if (internetUsage.Count > 20)
        {
            internetUsage.Remove(internetUsage.Keys.First());
        }
        bytesReceivedPrev = bytesReceived;
    }

    private void CheckInternetSpeed(DateTime now)
    {
        WebClient client = new WebClient();
        Uri URL = new Uri("http://sixhoej.net/speedtest/1024kb.txt");
        double starttime = Environment.TickCount;
        client.DownloadFile(URL, Constants.GetAppDataPath() + "\\" + now.Ticks);
        double endtime = Environment.TickCount;

        double secs = Math.Floor(endtime - starttime) / 1000;

        double secs2 = Math.Round(secs, 0);

        double kbsec = Math.Round(1024 / secs);

        double mbsec = kbsec / 100;

        internetSpeed.Add(now, mbsec);
        if (internetSpeed.Count > 20)
        {
            internetSpeed.Remove(internetSpeed.Keys.First());
        }
        client.Dispose();
        try
        {
            // delete downloaded file
            System.IO.File.Delete(Constants.GetAppDataPath() + "\\" + now.Ticks);
        }
        catch (Exception ex1)
        {
            Console.WriteLine(ex1.Message);
        }
    }

问题是,当我从互联网下载一些大型文件时(例如通过网络浏览器的blender安装程序),互联网速度系列正在低于我的互联网使用系列。 (见图) anomalies in my internet meter 坦率地说,我不太了解这个话题,也不知道我应该走哪个方向。

0 个答案:

没有答案