我正在使用 JNDI 连接到 LDAP,我需要它来处理许多同时的 TCP 连接。所有连接都需要同时建立。神秘的是,有时它可以工作,有时它只建立一些连接,例如 30 个中的 17 个。我通过谷歌搜索看到了类似的问题,但没有人提供他们的解决方案。谢谢!堆栈跟踪:
javax.naming.ServiceUnavailableException: 172.16.0.xxx:389; socket closed
at com.sun.jndi.ldap.Connection.readReply(Connection.java:464)
at com.sun.jndi.ldap.LdapClient.ldapBind(LdapClient.java:370)
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:222)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2752)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:328)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:205)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:223)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:166)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:96)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:695)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:318)
at javax.naming.InitialContext.init(InitialContext.java:253)
at javax.naming.InitialContext.<init>(InitialContext.java:227)
at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:112)
编辑:一些代码
socket = new Socket(hostname, port);
is = socket.getInputStream();
os = socket.getOutputStream();
bif = new BufferedInputStream(is);
bof = new BufferedOutputStream(os);
env = new Hashtable<String, String>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, usr);
env.put(Context.SECURITY_CREDENTIALS, pwd);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.REFERRAL,"follow");
env.put("java.naming.ldap.derefAliases", "never");
env.put(Context.PROVIDER_URL, "ldap:// /*address*/);
try {
ctx = new InitialDirContext(env);//ERROR
} catch (ServiceUnavailableException e1) {
e1.printStackTrace();
System.out.println("service unavailable");
}