Code example for Socket

Methods: connect, setKeepAlive, setReuseAddress, setSoLinger, setTcpNoDelay

0
  } 
 
  public static Socket newSocket(String host, int port, int timeout)
    throws IOException 
  { 
    final Socket socket = new Socket();
 
    socket.setReuseAddress(true);
    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);
    //socket.setSoTimeout(timeout); 
 
    return socket;
  } 
}