Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating bandwidth speed of data sent/received over a socket?

Does anyone have any tips on how to calculate the bandwidth usage of a socket?

For example, as I send data over a socket to the server I am connected to, I want to show the Kb/s that is being sent.

Google search didn't reveal anything useful. Maybe I'm searching the wrong terms.

like image 934
Bart Wegrzyn Avatar asked Aug 20 '10 18:08

Bart Wegrzyn


1 Answers

The best you're probably going to be able to easily do is to record when you start writing and then count bytes you've successfully sent to the Socket.getOutputStream.write() method. For a small amount of data, that will be very inaccurate as it's just filling up the OS's transmission buffer which will initially take bytes much faster than it actually sends them.

It should amortize to essentially the correct rate over a fairly large amount of data, however.

like image 134
Mark Peters Avatar answered Sep 23 '22 14:09

Mark Peters