Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.net.*; public class JavaSocketSetKeepAliveExample1 { public static void main(String[] args) throws IOException { Socket socket = new Socket(); //enabling the boolean value true boolean on=true; //setKeepAlive() method either enables or disables the SO_KEEPALIVE option socket.setKeepAlive(on); //getKeepAlive() returns a boolean indicating whether SO_KEEPALIVE option is enabled or not System.out.println("SO_KEEPALIVE is enabled: "+socket.getKeepAlive()); } }
Output