我在使用Android上的JmDNS解決mDNS服務時遇到問題。該服務偶爾會解決,但大部分時間只調用serviceAdded()而不調用serviceResolved()!有誰知道這個問題可能是什麼?如果有一些網絡配置我缺少或這樣?Android上JmDNS的網絡配置。已添加服務但未解決
我使用以太網(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);
}
我試圖解決問題那。它不適合我。相反,解決方案是切換到完美工作的mdnsjava。 – JohnyTex