Created
March 18, 2013 17:18
-
-
Save shanet/5188968 to your computer and use it in GitHub Desktop.
Revisions
-
shanet created this gist
Mar 18, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ import java.net.InetAddress; import java.net.Socket; import java.io.IOException; import java.net.UnknownHostException; public class Monitor { public static void main(String[] args) { if(args.length < 2) { System.out.println("Bad number of arguments."); return; } String server = args[0]; int port = Integer.valueOf(args[1]); try { InetAddress ina = InetAddress.getByName(server); if(ina.isReachable(3000)) { System.out.println("Server is up."); } else { System.out.println("Server is down!"); return; } } catch (IOException ioe) { ioe.printStackTrace(); return; } Socket socket = null; try { socket = new Socket(server, port); if(socket.isConnected()) { System.out.println("Service is up on port " + port + "."); } else { System.out.println("Service is down on port " + port + "!"); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if(socket != null && socket.isConnected()) { try { socket.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } return; } }