2012-10-07 102 views
1

給定我的Android設備在同一網絡上的計算機的IP地址,我必須找到它的NetBIOS/FQDN名稱......有沒有任何「乾淨」的解決方案用Android SDK完成這個工作,一般來說在java中完成?IP地址NetBIOS/FQDN名稱在Java/Android

顯然InetAddress.get *主機名不返回NetBIOS名稱:)

回答

4

您可以使用JCIFS開源庫。

InetAddress addr = NbtAddress.getByName("hostname").getInetAddress(); 

雙向工作,IP地址到主機名,反之亦然。

0

嘗試......

import java.util.Hashtable; 
import javax.naming.Context; 
import javax.naming.NamingEnumeration; 
import javax.naming.directory.SearchControls; 
import javax.naming.directory.SearchResult; 
import javax.naming.ldap.InitialLdapContext; 
import javax.naming.ldap.LdapContext; 

public class SearchNetBIOSName { 

    public static void main(String[] args) { 
     try { 
      Hashtable<String, String> env = new Hashtable<String, String>(); 
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
      env.put(Context.PROVIDER_URL, "ldap://my.domain.com:389"); 
      env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
      env.put(Context.SECURITY_PRINCIPAL, "cn=administrator,cn=users,dc=my,dc=domain,dc=com"); 
      env.put(Context.SECURITY_CREDENTIALS, "********"); 
      LdapContext context = new InitialLdapContext(env, null); 
      String searchBase = "cn=Partitions,cn=Configuration,dc=my,dc=domain,dc=com"; 
      String searchFilter = "(&(objectcategory=Crossref)(netbiosname=*))"; 
      SearchControls controls = new SearchControls(); 
      controls.setSearchScope(SearchControls.ONELEVEL_SCOPE); 
      NamingEnumeration answers = context.search(searchBase, searchFilter, controls); 
      while (answers.hasMore()) { 
       SearchResult rs = (SearchResult) answers.next(); 
       String netBiosName = rs.getAttributes().get("NetBIOSName").get(0).toString(); 
       System.out.println(netBiosName); 
      } 
      context.close(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

我應該用什麼來代替my.domain.com? –

+2

真是無稽之談...... – RumburaK

3

事實上,由湯姆所提供的代碼不能正常工作,此代碼對我的作品(含JCIFS庫。)

NbtAddress[] nbts = NbtAddress.getAllByAddress("IP ADDRESS AS STRING"); 
String netbiosname = nbts[0].getHostName(); 

回報的NetBios設備名字符串,如果成功,或者拋出的UnknownHostException如果目標不存在或沒有NetBios名稱。

+0

工作的很棒。謝謝Marek – Bill