diff --git a/src/main/java/seng302/gameServer/ServerAdvertiser.java b/src/main/java/seng302/gameServer/ServerAdvertiser.java index 6007f248..0b69b5a4 100644 --- a/src/main/java/seng302/gameServer/ServerAdvertiser.java +++ b/src/main/java/seng302/gameServer/ServerAdvertiser.java @@ -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 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 e = NetworkInterface.getNetworkInterfaces(); + while (e.hasMoreElements()) { + NetworkInterface ni = e.nextElement(); + if (ni.isLoopback()) + continue; + if(ni.isPointToPoint()) + continue; + if(ni.isVirtual()) + continue; + + Enumeration 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; + } } diff --git a/src/main/java/seng302/visualiser/ServerListener.java b/src/main/java/seng302/visualiser/ServerListener.java index c9614cf7..34b76fdb 100644 --- a/src/main/java/seng302/visualiser/ServerListener.java +++ b/src/main/java/seng302/visualiser/ServerListener.java @@ -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); } diff --git a/src/main/java/seng302/visualiser/controllers/StartScreenController.java b/src/main/java/seng302/visualiser/controllers/StartScreenController.java index 3b3d7c68..a02c6a74 100644 --- a/src/main/java/seng302/visualiser/controllers/StartScreenController.java +++ b/src/main/java/seng302/visualiser/controllers/StartScreenController.java @@ -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 e = NetworkInterface.getNetworkInterfaces(); - while (e.hasMoreElements()) { - NetworkInterface ni = e.nextElement(); - if (ni.isLoopback()) - continue; - if(ni.isPointToPoint()) - continue; - if(ni.isVirtual()) - continue; - Enumeration 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