i discovered this great library and i'm using it for my program.
is there a way to get the current download/upload speed per seconds?
thanks a lot
You can call getBytesSent() and getBytesRecv() twice with a short delay, dividing the difference by the difference in grtTimeStamp().
I tried this way but result is always 0
long download1 = net.getBytesRecv();
Thread.sleep(1000);
long download2 = net.getBytesRecv();
System.out.println("prova " + (download2 - download1)/net.getTimeStamp());
You need to update the network stats between reads.
Specifically, call net.updateNetworkStats() after the sleep. And save/subtract the first time stamp too.
Three things:
1) You need to update the network stats
2) You need to divide the amount of bytes received on the time that has passed
3) You'll get better results from sleeping/waiting for a bit longer.
There's a few interesting facts of Java, one of them being that asking for a sleep time of Nms won't necessarily have the thread sleep for that amount of time (it's more along the line of asking the thread to "sleep as close to Nms as you are able").
The code should look more like:
long download1 = net.getBytesRecv();
long timestamp1 = net.getTimeStamp();
Thread.sleep(2000); //Sleep for a bit longer, 2s should cover almost every possible problem
net.updateNetworkStats(); //Updating network stats
long download2 = net.getBytesRecv();
long timestamp2 = net.getTimeStamp();
System.out.println("prova " + (download2 - download1)/(timestamp2 - timestamp1));
//Do the correct calculations
And I see @dbwiddis got to it first
First of all thank you
@YoshiEnVerde I just tried your code but the result looks wrong. If the result is in bytes per second, 6064 bytes are too much compared to the 45 megabit bandwidth used.
Should not have come about a result around 5625?

If I look at Windows task manager, the megabits in downloads are higher than those shown by speedtest.
thank you so much guys

is there a way to detect default network card?
EDIT: i found solution here