public List < InetAddress > discoverHosts ( int udpPort , int timeoutMillis ) { List < InetAddress > hosts = new ArrayList < InetAddress > ( ) ; DatagramSocket socket = null ; try { socket = new DatagramSocket ( ) ; broadcast ( udpPort , socket ) ; socket . setSoTimeout ( timeoutMillis ) ; while ( true ) { DatagramPacket packet = discoveryHandler . onRequestNewDatagramPacket ( ) ; try { socket . receive ( packet ) ; } catch ( SocketTimeoutException ex ) { if ( INFO ) info ( "kryonet" , "Host discovery timed out." ) ; return hosts ; } if ( INFO ) info ( "kryonet" , "Discovered server: " + packet . getAddress ( ) ) ; discoveryHandler . onDiscoveredHost ( packet , getKryo ( ) ) ; hosts . add ( packet . getAddress ( ) ) ; } } catch ( IOException ex ) { if ( ERROR ) error ( "kryonet" , "Host discovery failed." , ex ) ; return hosts ; } finally { if ( socket != null ) socket . close ( ) ; discoveryHandler . onFinally ( ) ; } }
Broadcasts a UDP message on the LAN to discover any running servers .