0

I need to set or get RTT in socket(AF_INET, SOCK_RAW, IPPROTO_TCP) way.

What do I need to do next to control such RTT in socket programming? In other words, how to find such RTT parameter?

3
  • There's no function for that in the API. Just send a message to the other end, have the other end send it back, and time it. Not so hard. Commented Jun 22, 2015 at 11:56
  • Will tcp-timestamp option help? Commented Jun 22, 2015 at 12:14
  • It isn't set, it's measured, by TCP. Commented Jun 22, 2015 at 12:39

2 Answers 2

3

On Linux you can get the RTT by calling getsockopt() with TCP_INFO:

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

/* ... */

tcp_info info;
socklen_t tcp_info_length = sizeof info;
ret = getsockopt(sock, SOL_TCP, TCP_INFO, &info, &tcp_info_length);
printf("rtt: %u microseconds\n", info.tcpi_rtt);
Sign up to request clarification or add additional context in comments.

4 Comments

Can I get the rtt time of accepted socket like this
@user786 Yes, that is the RTT as calculated by the TCP stack.
so where I call getsockopt() is it after the call to accept() or after the call to read(..)
@user786 It will work in both places, but the value will change as the socket is used, because it is an estimation calculated as new packets are sent and confirmed through that connection. I am not sure read() will have any effect on the value.
1

To measure the Round Trip Time (RTT) write a simple client-server application where one node:

  1. Reads the current time with clock_gettime()
  2. Sends a message to the other node using write() on the (already opened) socket
  3. Waits the message back using read()
  4. Reads the current time using clock_gettime()

RTT is the difference between the two times.

2 Comments

POSIX.1-2008 marks gettimeofday(2) as obsolescent. clock_gettime(2) should be used instead (or time(2)).
Right. Fixed. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.