如何衡量互联网带宽

时间:2011-06-10 21:08:27

标签: java sockets stream bandwidth

我遇到了问题但无法找到答案。我想用java测量互联网带宽,但我不知道如何。

获得一些提示会很棒;我知道我必须打开一个套接字并将其发送到一个已定义的服务器,然后将其恢复,然后再使用它。

但是我该怎么编码呢?

2 个答案:

答案 0 :(得分:5)

我只需下载固定大小的文件即可实现。没有经过测试,但这些方面的内容应该可以正常工作

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.

答案 1 :(得分:1)

如何固定任意数量的时间并发送关于它的数据?

例如,假设我希望我的服务器将其带宽使用限制为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;
    }
}
相关问题