InetAddress serverAddr = InetAddress.getByName(serverAddress); String hostname = serverAddr.getCanonicalHostName(); Socket socket = new Socket(serverAddr, portNumber); // Freezes before this line if the server is unavailable socket.setSoTimeout(3000);
Does anyone know how to implement the check of server availability or prevent the freezing?
By using the two-argument constructor, you tell Java to connect immediately. What you are looking for is likely
This will still block for 3s though. If you want to prevent that, use a thread for the connection.