我无法在Android上使用JmDNS解析mDNS服务。该服务偶尔会解析,但大多数时候只调用serviceAdded()而不是serviceResolved()!有谁知道问题可能是什么?如果有一些网络配置我缺少或者这样?
我使用以太网( eth1 ),配置如下:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:18:7d:32:77:40 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.191/24 brd 172.18.0.255 scope global eth0
inet6 2a00:801:19:1:218:7dff:fe32:7740/64 scope global dynamic
valid_lft 2591918sec preferred_lft 604718sec
inet6 fe80::218:7dff:fe32:7740/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop state DOWN
link/sit 0.0.0.0 brd 0.0.0.0
5: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:14:d1:b0:7b:89 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.162/24 scope global eth1
inet6 fe80::214:d1ff:feb0:7b89/64 scope link
valid_lft forever preferred_lft forever
Routing table:
172.18.0.1 dev eth0 scope link
224.0.0.251 dev eth1 scope link
192.168.3.0/24 dev eth1 proto kernel scope link src 192.168.3.162
172.18.0.0/24 dev eth0 proto kernel scope link src 172.18.0.191 metric 202
default via 172.18.0.1 dev eth0
default via 172.18.0.1 dev eth0 metric 202
以下是我正在使用的代码:
try {
InetAddress address = InetAddress.getByName("192.168.3.162");
jmDNS = JmDNS.create(address);
serviceListener = new ServiceListener() {
@Override
public void serviceResolved(ServiceEvent serviceEvent) {
String additions = "";
if (serviceEvent.getInfo().getInetAddresses() != null && serviceEvent.getInfo().getInetAddresses().length > 0) {
additions = serviceEvent.getInfo().getInetAddresses()[0].getHostAddress();
}
Log.i("GPSservice.NetworkOperations", "Service resolved: " + serviceEvent.getInfo().getQualifiedName() + " port:" + serviceEvent.getInfo().getPort() + ", " + additions);
}
@Override
public void serviceRemoved(ServiceEvent serviceEvent) {
Log.i("GPSservice.NetworkOperations", "Service removed: " + serviceEvent.getName());
}
@Override
public void serviceAdded(ServiceEvent serviceEvent) {
Log.i("GPSservice.NetworkOperations", "service added");
jmDNS.requestServiceInfo(INVENTORY_TYPE, serviceEvent.getName());
}
};
jmDNS.addServiceListener(INVENTORY_TYPE, serviceListener);
} catch (Exception e) {
Log.e("GPSservice.NetworkOperations", "JmDNS error.", e);
}
答案 0 :(得分:0)
我遇到了同样的问题。
在这里找到答案:JmDNS: Can't resolve service
基本上只需从SourceForge SVN repo中下载3.4.1的最新源代码,这将解决解决服务的问题