Code example for Socket

Methods: connect, setKeepAlive, setReuseAddress, setSoLinger, setTcpNoDelay

  public static Socket newSocket(String host, int port, int timeout)
    throws IOException 
    final Socket socket = new Socket();
    socket.setKeepAlive(true);  //Will monitor the TCP connection is valid
    socket.setTcpNoDelay(true);  //Socket buffer Whetherclosed, to ensure timely delivery of data
    socket.setSoLinger(true, 0);  //Control calls close () method, the underlying socket is closed immediately
    socket.connect(new InetSocketAddress(host, port), timeout);
    return socket;