Skip to content

Instantly share code, notes, and snippets.

@zonia3000
Created September 11, 2016 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zonia3000/7d8f219461de3100665c65102d84c8e6 to your computer and use it in GitHub Desktop.
Save zonia3000/7d8f219461de3100665c65102d84c8e6 to your computer and use it in GitHub Desktop.
/**
* Socket factory with keep alive setting.
* For using with com.sun.jndi.ldap.LdapCtxFactory add it in the env Hastable:
* env.put("java.naming.ldap.factory.socket", "yourpackage.KeepAliveSocketFactory");
*/
public class KeepAliveSocketFactory extends SocketFactory {
@Override
public Socket createSocket() throws IOException {
Socket socket = new Socket();
socket.setKeepAlive(true);
return socket;
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
Socket socket = new Socket(host, port);
socket.setKeepAlive(true);
return socket;
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
Socket socket = new Socket(address, port, localAddress, localPort);
socket.setKeepAlive(true);
return socket;
}
@Override
public Socket createSocket(String host, int port) throws IOException, SocketException {
Socket socket = new Socket(host, port);
socket.setKeepAlive(true);
return socket;
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
Socket socket = new Socket(host, port, localHost, localPort);
socket.setKeepAlive(true);
return socket;
}
public static SocketFactory getDefault() {
synchronized (KeepAliveSocketFactory.class) {
return new KeepAliveSocketFactory();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment