如何衡量互联网带宽

J2A*_*J2A 3 java sockets bandwidth stream

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

得到一些提示会很棒; 我知道我必须打开一个套接字并将其发送到已定义的服务器,将其取回然后使用时间.

但是我该怎么编码呢?

Voo*_*Voo 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.
Run Code Online (Sandbox Code Playgroud)