Skip to content

Instantly share code, notes, and snippets.

@shanet
Created March 18, 2013 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanet/5188968 to your computer and use it in GitHub Desktop.
Save shanet/5188968 to your computer and use it in GitHub Desktop.
A quick Java program to check if a server is up or a service on a server is up.
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;
}
}
@cfpwastaken
Copy link

Not working

@shanet
Copy link
Author

shanet commented Jul 5, 2020

Not surprising, this is from seven years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment