Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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