0

这就是问题所在:我需要编写一个程序,该程序将测量我的互联网速度以及我的计算机当前使用的速度,并将其放入包含两个系列的折线图中。在 google 和 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);
        }
    }

问题是,当我从互联网下载一些大文件(例如通过网络浏览器的搅拌机安装程序)时,互联网速度系列低于我的互联网使用系列。(见图) 我的互联网仪表异常 坦率地说,我不太了解这个话题,也不知道我应该往哪个方向发展。

4

0 回答 0