Reputation: 295
I want to write simple unix-application that measures tcp-connection speed. So I have:
I thought that measurement on server is somthing like this:
clock_gettime(CLOCK_REALTIME, &start);
size = recv(csocket_fd, buf, BUFFER_SIZE, 0);
clock_gettime(CLOCK_REALTIME, &end);
but it seems like it's wrong way.
any suggestions?
Upvotes: 1
Views: 1359
Reputation: 73051
On the server, when you receive the first data from the client, record the current time to a variable.
Also on the server, whenever you receive data from the client, add the number of bytes received to a counter variable.
Then at any time you want, you can calculate the cumulative average bytes-per-second speed of the connection by calculating (total_bytes_received)/(current_time - first_data_received_time); (Watch out for a potential divide by zero if current_time and first_data_received_time are equal!)
If you want to do something more elaborate, like a running average over the last 10 seconds, that's a little more involved, but computing the cumulative average is pretty easy.
Upvotes: 2
Reputation: 354
I have done a few assignments in networking and one thing that i noticed it is that it wouldn't work the way you are trying to. We have to complete a send-receive for the server to receive again and there are other factors that prevent us from doing it ... from MTUs to buffer sizes etc. I have used is netperf to benchmark bandwidths before[is this the speed you are talking about ?]. The code is open source.
Upvotes: 0