Java API Tutorial - Java Socket.setKeepAlive(boolean on)








Syntax

Socket.setKeepAlive(boolean on) has the following syntax.

public void setKeepAlive(boolean on)   throws SocketException

Example

In the following code shows how to use Socket.setKeepAlive(boolean on) method.

//  ww w .  j ava 2 s .c  o m
import java.net.Socket;

public class Main {
  public static void main(String[] args) throws Exception {
    Socket client = new Socket("google.com", 80);
    client.setKeepAlive(true);
    System.out.println(client.getKeepAlive());

    client.close();
  }
}

The code above generates the following result.