好的,我一直在寻找从本地子网获取DNS服务的方法,但似乎无法使其正常工作。
到目前为止,当我使用以下代码连接到wifi时,我可以获得本地DNS ip:
ArrayList<String> servers = new ArrayList<String>();
try {
Class<?> SystemProperties = Class
.forName("android.os.SystemProperties");
Method method = SystemProperties.getMethod("get",
new Class[] { String.class });
for (String name : new String[] { "net.dns1", "net.dns2",
"net.dns3", "net.dns4", }) {
String value = (String) method.invoke(null, name);
if (value != null && !"".equals(value)
&& !servers.contains(value))
servers.add(value);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (String s : servers) {
Log.v("ERRORID", s);
}
并以字符串形式返回以下内容:
10.10.10.204
172.19.2.10
从这里我想查询每个DNS ip的所有服务,但不知道如何。
编辑: 我尝试过使用jmDNS,但没有运气
InetAddress addr = InetAddress.getByName("172.19.2.10");
JmDNS jmdns = JmDNS.create(addr);
jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
SampleListener()是:
static class SampleListener implements ServiceListener {
@Override
public void serviceAdded(ServiceEvent event) {
Log.v("ERRORID", "Service added : " + event.getName() + "."
+ event.getType());
}
@Override
public void serviceRemoved(ServiceEvent event) {
Log.v("ERRORID", "Service removed : " + event.getName() + "."
+ event.getType());
}
@Override
public void serviceResolved(ServiceEvent event) {
Log.v("ERRORID", "Service resolved: " + event.getInfo());
}
}
答案 0 :(得分:0)
Jmdns.create仅绑定到本地网络接口。不是远程DNS服务器。使用PC的任何网络接口的IP,jmds将仅在此接口上运行。 您不必查询服务器以获取服务。他们应该自动发布它们,你应该得到一个serviceAddedEvent调用。