0

I am trying to set the TCP Keep Alive Socket option and adjust some of its value using the NDK, at this point, I think the NDK version is irrelevant.

The relevant code is C++, is called from a method in a class and looks like the following:

// Assume the socket handle is valid and was created with socket()
// Enable TCP Keep Alive
int nOptVal = 1;
setsockopt(hSocket, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<char*>(&nOptVal), sizeof(nOptVal));

// Every 15 secs.
nOptVal = 15;
setsockopt(hSocket, SOL_TCP, SO_KEEPINTVL, reinterpret_cast<char*>(&nOptVal), sizeof(nOptVal));

// Send a single probe
nOptVal = 1;
setsockopt(hSocket, SOL_TCP, SO_KEEPCNT, reinterpret_cast<char*>(&nOptVal), sizeof(nOptVal));

When I try to compile the above, the SO_KEEPINTVL and SO_KEEPCNT macros are unfound and I cannot seem to find their definition in the include files of the NDK.

As anyone ever made this work?

One last note, I cannot do it from the java code since the options need to be set on sockets that are inside a library the java code is using.

I looked through SO and found this question which explains how to do it in Java so it should be possible, I guess.

Thanks for any help on this.

Community
  • 1
  • 1
woodleg.as
  • 274
  • 1
  • 4
  • 15
  • Because you use the wrong macros, see e.g. [this document](http://www.tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/#setsockopt). The correct names are even mentioned in the SO link you have. – Some programmer dude Aug 21 '14 at 22:21
  • Indeed, sorry about that should have double checked before posting. Anyway if it may help someone the include for these value is `linux/tcp.h`. – woodleg.as Aug 21 '14 at 22:51

1 Answers1

2

To sum up, correct macro names are TCP_KEEPINTVL and TCP_KEEPCNT. They are defined in the linux/tcp.h file.

woodleg.as
  • 274
  • 1
  • 4
  • 15