title: Connect to remote AD LDS server using Java
body: <p>I have an azure VM, on which I have hosted an AD LDS instance. I have also created a few users in an OU. I am trying to connect to the AD LDS instance using my Java code. My code is as follows:</p>
  
  <pre><code>public final class Main {
  
      private static final List&lt;String&gt; EMPTY_ARGS = emptyList();
  
      public static void main(String[] args) throws Exception {
  
          List&lt;String&gt; argsList = asList(args);
  
          String url = "LDAP://xx.xx.xxx.xx:389/o=org,c=US";   //Public IP of VM
          String principal = "CN=Test User,OU=Users,O=org,C=US";
          String password = "password";
          List&lt;String&gt; attributeNames = new ArrayList&lt;String&gt;();
  
          attributeNames.add(url);
          attributeNames.add(principal);
          attributeNames.add(password);
  
          System.out.println("url = " + url);
          System.out.println("principal = " + principal);
          System.out.println("password = " + password);
          System.out.println("attributeNames = " + attributeNames);
  
          Hashtable&lt;Object, Object&gt; env = new Hashtable&lt;Object, Object&gt;();
          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
          env.put(Context.PROVIDER_URL, url);
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          env.put(Context.SECURITY_PRINCIPAL, principal);
          env.put(Context.SECURITY_CREDENTIALS, password);
  
          env.put("java.naming.ldap.factory.socket", "cnauroth.ldaputil.CustomSSLSocketFactory");
  
          DirContext ctx = null;
  
          try {
              System.out.println("Inside");
              ctx = new InitialDirContext(env);
  
              System.out.println("Successfully authenticated.");
  
              if (!attributeNames.isEmpty()) {
                  Attributes attributes = ctx.getAttributes(principal,
                      attributeNames.toArray(new String[attributeNames.size()]));
  
                  for (String attributeName : attributeNames) {
                      Attribute attribute = attributes.get(attributeName);
  
                      if (null != attribute) {
                          NamingEnumeration&lt;?&gt; values = attribute.getAll();
  
                          if (null != values) {
                              if (values.hasMore()) {
                                  System.out.println(attributeName);
  
                                  while (values.hasMore())
                                      System.out.println("    " + values.next());
                              }
                          }
                      }
                  }
              }
          }
          finally {
              if (null != ctx) {
                  try {
                      ctx.close();
                  }
                  catch (NamingException e) {
                      e.printStackTrace(System.err);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>When i execute this code from outside the VM, I get the following error:</p>
  
  <pre><code>Exception in thread "main" javax.naming.CommunicationException: 191.238.241.11:389 [Root exception is java.net.ConnectException: Connection timed out: connect]
      at com.sun.jndi.ldap.Connection.&lt;init&gt;(Unknown Source)
      at com.sun.jndi.ldap.LdapClient.&lt;init&gt;(Unknown Source)
      at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
      at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
      at com.sun.jndi.ldap.LdapCtx.&lt;init&gt;(Unknown Source)
      at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
      at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
      at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
      at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
      at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
      at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.init(Unknown Source)
      at javax.naming.InitialContext.&lt;init&gt;(Unknown Source)
      at javax.naming.directory.InitialDirContext.&lt;init&gt;(Unknown Source)
      at cnauroth.ldaputil.Main.main(Main.java:92)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.&lt;init&gt;(Unknown Source)
      at sun.security.ssl.SSLSocketFactoryImpl.createSocket(Unknown Source)
      at cnauroth.ldaputil.CustomSSLSocketFactory.createSocket(CustomSSLSocketFactory.java:72)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
      ... 15 more
  </code></pre>
  
  <p>what may be the reason for the error? if i run a similar code on the VM itself, I am able to connect to the AD LDS instance</p>