开发者

how to see properties of a JmDNS service in reciever side?

开发者 https://www.devze.com 2023-04-05 03:54 出处:网络
One way of creating JmDNS services is : ServiceInfo.create(type, name, port, weight, priority, props);

One way of creating JmDNS services is :

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

where开发者_如何学Go props is a Map which describes some propeties of the service. Does anybody have an example illustrating the use of theese properties, for instance how to use them in the reciever part. I've tried :

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);

but, then in a machine receiving this service, what can I do to see those properties?

I would apreciate any help...


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);
}


It has been a while since this was asked but I had the same question. One problem with the original question is that the host and ports should not be put into the text field, and in this case there should actually be two service types one secure and one insecure (or perhaps make use of subtypes).

Here is an incomplete example that gets a list of running workstation services:

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.
    ...
  }
}

Due to the way that JmDNS is implemented the first call to list() on a given type is slow (several seconds) but subsequent calls will be pretty fast. Providers of services can change the properties by calling info.setText(settings) and the changes will be propagated out to the listeners automatically.

0

精彩评论

暂无评论...
验证码 换一张
取 消