* @param udpPort The UDP port of the server.
* @param timeoutMillis The number of milliseconds to wait for a response.
* @return the first server found, or null if no server responded.
*/
public InetAddress discoverHost (int udpPort, int timeoutMillis) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
broadcast(udpPort, socket);
socket.setSoTimeout(timeoutMillis);
DatagramPacket packet = new DatagramPacket(new byte[0], 0);
try {
socket.receive(packet);
} catch (SocketTimeoutException ex) {
if (INFO) info("kryonet", "Host discovery timed out.");
return null;
}
if (INFO) info("kryonet", "Discovered server: " + packet.getAddress());
return packet.getAddress();
} catch (IOException ex) {
if (ERROR) error("kryonet", "Host discovery failed.", ex);
return null;
} finally {
if (socket != null) socket.close();
}
}