8

In C/Linux, it's easily to set different value about those socket options for every KEEPALIVE tcp connection independently.

TCP_KEEPCNT (since Linux 2.4) The maximum number of keepalive probes TCP should send before dropping the connection. This option should not be used in code intended to be portable.

TCP_KEEPIDLE (since Linux 2.4) The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.

TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.

In netty or java, how to set the three socket options for socket? I know there is no portable way to solve it, but only in Linux, can I set those socket options?

Yang Juven
  • 314
  • 1
  • 3
  • 8
  • 1
    As mentioned in your documentation above, these options "[...]should not be used in code intended to be portable". You'd need to use JNI (or better, JNA) and implement your own `Socket`/`SocketFactory` if you want to use these – fge Mar 18 '14 at 07:35
  • refer http://stackoverflow.com/questions/6161437/setsockopt-in-java-programs – Rahul R Dhobi Mar 18 '14 at 11:05
  • Thanks all. It's need to use JNA to implement it. – Yang Juven Mar 19 '14 at 00:06
  • For C++, it's described in this post: [Socket keepalive not working](http://stackoverflow.com/a/21867895/514235). – iammilind May 26 '16 at 10:51

3 Answers3

4

The link given doesn't actually tell you how to use JNA to implement it. Having spent a while digging around on SO to find solutions, I wrote my own library. You can find it here.

abligh
  • 24,573
  • 4
  • 47
  • 84
4

It appears this is supported in Java 11, via new fields in the ExtendedSocketOptions class. These can be passed to the setOption method on either java.net.Socket or java.nio.channels.SocketChannel.

Note I have not actually tried using this. The docs explicitly say these are platform specific, so you'll need to test they actually do what you want on the platforms you care about.

import java.net.Socket;
import jdk.net.ExtendedSocketOptions;

Socket socket = new Socket();
socket.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, 10);
socket.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, 2);
socket.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, 3);
Erik van Velzen
  • 6,211
  • 3
  • 23
  • 23
Lachlan
  • 3,744
  • 2
  • 26
  • 29
  • 1
    Apparently this was also backported to some Java 8 distributions during 2020 for certain platforms like Linux and Mac OS. Source: https://bugs.openjdk.java.net/browse/JDK-8194298 and https://mail.openjdk.java.net/pipermail/jdk8u-dev/2020-October/012817.html – JohannesB Jul 13 '21 at 16:59
1

Recent versions of Netty allow you to use epoll type channels and set Linux-native socket options such as the ones you mentioned.

See the documentation of EpollChannelOption for details.

paprika
  • 2,424
  • 26
  • 46