Question:

Setting the "ping" time of setKeepAlive() in Java?

by  |  earlier

0 LIKES UnLike

I am using the [socket].setKeepAlive(true) to keep a socket connection open in my Java program. I read that this method sends data after every 2 hours of inactivity, but the thing I'm connected to will disconnect automatically after 20 minutes.

Is there a way to change the time interval of the setKeepAlive method?

 Tags:

   Report

1 ANSWERS


  1. No, and this won't solve your problem. This is a transport-level keep alive used to detect network failures. Even if you could set it to, say, 5 minutes, it would just detect network failures within 5 minutes and shutdown the connection.

    The thing you're connected to will disconnect after 20 minutes if there is no application-level activity. Transport-level activity won't stop it from disconnecting you.

    You need to actually do something with the connection. What is the thing you're connecting to and what protocol does it use? Why do you need to stay connected for so long rather than creating a new connection?

    The correct solution depends on the protocol. However, do not confuse TCP keep-alives (which only detect network connection failure) with application pings (which prevent timeouts).

    Edit: If the protocol is telnet, and you want to keep the connection open, you need to send some telnet protocol data. The telnet protocol offers NOP (no operation) for just this purpose. See RFC854. You can also repeat an already-agreed negotiation, for example, 'IAC WILL TRANSMIT-BINARY' when binary transmission is already agreed upon. See RFC855/856.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions