2011-09-16 27 views
0

一種方法是:如何查看接收方的JmDNS服務屬性?創建JmDNS服務的

ServiceInfo.create(type, name, port, weight, priority, props); 

其中道具是描述服務的一些化子性質的地圖。有沒有人舉例說明使用這些屬性,例如如何在接收器部分使用它們。 我已經試過:

Hashtable<String,String> settings = new Hashtable<String,String>(); 
settings.put("host", "hhgh"); 
settings.put("web_port", "hdhr"); 
settings.put("secure_web_port", "dfhdyhdh"); 
ServiceInfo info = ServiceInfo.create("_workstation._tcp.local.", "service6", 80, 0, 0, true, settings); 

而且,然後在機器接受這種服務,我能做些什麼來看看這些屬性?

我會apreciate任何幫助......

回答

0

它已經有一段時間,因爲這是問,但我有同樣的問題。原始問題的一個問題是主機和端口不應該放在文本字段中,在這種情況下實際上應該有兩種服務類型,一種安全和一種不安全(或者可能使用子類型)。

下面是一個不完整的例子,得到的運行工作站服務的列表:在給定類型

ServiceInfo[] serviceInfoList = jmdns.list("_workstation._tcp.local."); 
if(serviceInfoList != null) { 
    for (int index = 0; index < serviceInfoList.length; index++) { 
    int port = serviceInfoList[index].getPort(); 
    int priority = serviceInfoList[index].getPriority(); 
    int weight = serviceInfoList[index].getWeight(); 
    InetAddress address = serviceInfoList[index].getInetAddresses()[0]; 
    String someProperty = serviceInfoList[index].getPropertyString("someproperty"); 

    // Build a UI or use some logic to decide if this service provider is the 
    // one you want to use based on prority, properties, etc. 
    ... 
    } 
} 

由於該JmDNS被實現爲列表(在第一次調用的方式)慢(幾秒鐘)但隨後的電話會很快。服務提供者可以通過調用info.setText(settings)來更改屬性,並且更改將自動傳播給偵聽器。

0
ServiceInfo info = jmDNS.getServiceInfo(serviceEvent.getType(), serviceEvent.getName()); 

Enumeration<String> ps = info.getPropertyNames(); 

while (ps.hasMoreElements()) { 
    String key = ps.nextElement(); 
    String value = info.getPropertyString(key); 
    System.out.println(key + " " + value); 
}