Fixed bug where discovery wasn't working under windows

- getLocalHost() was returning the networks public IP address, changed this to getByName() for the local IP

Tags: #story[1247]
This commit is contained in:
Michael Rausch
2017-09-02 02:16:40 +12:00
parent 3f910b8db7
commit eb1d3f1a60
3 changed files with 44 additions and 38 deletions
@@ -3,7 +3,10 @@ package seng302.gameServer;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Hashtable;
/**
@@ -33,7 +36,7 @@ public class ServerAdvertiser {
private Hashtable<String ,String> props;
private ServerAdvertiser() throws IOException{
jmdnsInstance = JmDNS.create(InetAddress.getLocalHost());
jmdnsInstance = JmDNS.create(InetAddress.getByName(getLocalHostIp()));
props = new Hashtable<>();
props.put("map", "");
@@ -115,4 +118,39 @@ public class ServerAdvertiser {
if (serviceInfo != null)
jmdnsInstance.unregisterService(serviceInfo);
}
/**
* Gets the local host ip address.
*
* @return the localhost ip address
*/
public static String getLocalHostIp() {
String ipAddress = null;
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
if (ni.isLoopback())
continue;
if(ni.isPointToPoint())
continue;
if(ni.isVirtual())
continue;
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if(address instanceof Inet4Address) { // skip all ipv6
ipAddress = address.getHostAddress();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (ipAddress == null) {
System.out.println("[HOST] Cannot obtain local host ip address.");
}
return ipAddress;
}
}