Search code examples
c++ioscsocketsraw-sockets

Reading TCP_NODELAY using getsockopt returning a weird value


I am trying to verifying that my setting of TCP_NODELAY is working by reading it back after I set it.

Im setting the value to '1', but when I read it back, its set to '4'. Im afraid im doing something wrong.

Heres my code:

    int tcpBefore;
    socklen_t tcpBeforeLen = sizeof(tcpBefore);
    int res = getsockopt(socket, IPPROTO_TCP, TCP_NODELAY, &tcpBefore, &tcpBeforeLen);

    // Turn on TCP no delay
    int tcpNoDelay = 1;
    res = setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (void *)&tcpNoDelay, sizeof(tcpNoDelay));

    int tcpAfter;
    socklen_t tcpAfterLen = sizeof(tcpAfter);
    res = getsockopt(socket, IPPROTO_TCP, TCP_NODELAY, &tcpAfter, &tcpAfterLen);

The value I get for 'tcpBefore' is '0'. The value I get after I set it is '4'. This seems odd.

Is there something I am doing wrong?


Solution

  • This is ok. The only thing relevant is that the flag is zero or non-zero. I'm pretty sure ios (that you tagged the question with) will behave more or less like the old Darwin code and from reading that I can see that when setting the option, the kernel only cares if it's zero or not, but when reading it the actual internal flag value is used.

    If you have kernel include files you can find the relevant flag in netinet/tcp_var.h (this is kernel include file, so don't include it yourself) and it's called TF_NODELAY and happens to have the value 4.