【问题标题】:How to measure internet bandwidth如何测量互联网带宽
【发布时间】:2011-09-12 19:34:27
【问题描述】:

我有一个问题,找不到答案。我想用 java 测量互联网带宽,但我不知道如何。

如果能得到一些提示就好了;我知道我必须打开一个套接字并将其发送到定义的服务器,取回它然后使用时间。

但是我该如何编码呢?​​

【问题讨论】:

  • 你说的是java程序本身使用的带宽吗?

标签: java sockets stream bandwidth


【解决方案1】:

我会通过下载一个固定大小的文件来实现这一点。未经测试,但这些方面的东西应该可以正常工作

byte[] buffer = new byte[BUFFERSIZE];
Socket s = new Socket(urlOfKnownFile);
InputStream is = s.getInputStream();
long start = System.nanoTime();
while (is.read(buffer) != -1) continue;
long end = System.nanoTime();
long time = end-start;
// Now we know that it took about time ns to download <filesize>. 
// If you don't know the correct filesize you can obviously use the total of all is.read() calls.

【讨论】:

    【解决方案2】:

    固定任意时间量并发送有关它的数据怎么样?

    例如,假设我希望我的服务器将其带宽使用限制为 100Bytes/s。 所以我固定 1 秒并发送数据,只要它不超过 1 秒和 100 字节。

    这里有一些伪代码来说明我在说什么:

    timer_get (a);
    sent_data = 0;
    
    while (not_finished_sending_data)
    {
        timer_get (b);
        if ((b - a) < 1 ) // 1 second
        {
            if (sent_data < 100) // 100 bytes
            {
                // We actually send here
                sent_data += send();
            }
        }
        else
        {
            timer_get (a);
            sent_data = 0;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2022-11-19
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多