Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.net.*; public class JavaSocketSetKeepAliveExample2 { public static void main(String[] args) throws IOException { Socket socket = new Socket(); //enabling the boolean value true boolean on=false; //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 if(socket.getKeepAlive()){ System.out.println("SO_KEEPALIVE option is enabled"); } else{ System.out.println("SO_KEEPALIVE option is disabled"); } } }
Output