如何测量互联网带宽

发布于 2024-11-14 16:52:25 字数 120 浏览 3 评论 0原文

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

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

但我该如何编码呢?

I have a problem and can't find answers. I would like to measure internet bandwidth with java, but I don´t know how.

It would be great to get some hints; I know that I have to open a socket and send it to a defined server, get it back and then use the time.

But how would I code this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软的没边 2024-11-21 16:52:25

好吧,我只需下载固定大小的文件即可实现此目的。未经测试,但按照这些思路应该可以正常工作

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.

Well I'd implement this simply by downloading a fixed size file. Not tested, but something along these lines should work just fine

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.
溺深海 2024-11-21 16:52:25

固定任意时间并发送尊重它的数据怎么样?

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

How about fixing an arbitrary amount of time and send the data respecting it?

For example, let's say i want my server to limit it's bandwidth usage to 100Bytes/s.
So i fix 1 second and send the data as long as it does not goes beyond 1 second and 100 Bytes.

Here's some pseudocode to show what I'm talking about:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文