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
@@ -12,6 +12,8 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import static seng302.gameServer.ServerAdvertiser.getLocalHostIp;
/**
* Listens for servers on the local network
*/
@@ -86,7 +88,7 @@ public class ServerListener{
}
private ServerListener() throws IOException {
jmdns = JmDNS.create(InetAddress.getLocalHost());
jmdns = JmDNS.create(InetAddress.getByName(getLocalHostIp()));
listener = new GameServeMonitor();
jmdns.addServiceListener(ServerAdvertiser.SERVICE_TYPE, listener);
}
@@ -9,6 +9,7 @@ import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import seng302.gameServer.ServerAdvertiser;
import seng302.gameServer.ServerDescription;
import seng302.visualiser.GameClient;
import seng302.visualiser.ServerListener;
@@ -94,7 +95,7 @@ public class StartScreenController implements Initializable, ServerListenerDeleg
public void hostButtonPressed() {
// new GameState(getLocalHostIp());
gameClient = new GameClient(holder);
gameClient.runAsHost(getLocalHostIp(), 4942);
gameClient.runAsHost(ServerAdvertiser.getLocalHostIp(), 4942);
// try {
//// String ipAddress = InetAddress.getLocalHost().getHostAddress();
//// new GameState(ipAddress);
@@ -155,42 +156,7 @@ public class StartScreenController implements Initializable, ServerListenerDeleg
// this.controller = controller;
// }
/**
* Gets the local host ip address and sets this ip to ClientState.
* Only runs by the host.
*
* @return the localhost ip address
*/
private 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.");
}
// ClientState.setHostIp(ipAddress);
return ipAddress;
}
/**
* Update the server list with new information