Advertise Here

Author Topic: Simple server checker  (Read 5418 times)

0 Members and 1 Guest are viewing this topic.

Offline Mdog

  • SMF For Free Member
  • *
  • Posts: 48
    • View Profile

  • Total Badges: 11
    Badges: (View All)
    Topic Starter Combination Level 2 Level 1 10 Posts
Simple server checker
« on: January 28, 2009, 07:29:55 am »
Well, I got bored and made a simple server checker.  It can be used to check if a server of any kind is online or not (webserver, irc, ect.).  It uses the Scanner class (in the util package) to get the users input.  I have also documented all of my code for those of you would like to learn how it works.

Code: [Select]
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author Mdog
 *
 * A small class made to check if any server is online (webserver, irc, ect.)
 *
 * I have decided to use the Scanner class to get the user's input instead of a gui.  CBA to write the gui stuff xD
 * I also decided to comment all the code for those of you who wish to learn
 */
public class Main {
    /**
     * Initilizing the scanner class
     */
    static Scanner scan = new Scanner(System.in);
    /**
     * The main method.  This is what the JVM looks for when it starts up.
     */
    public static void main(String[] args) {
        // Displaying a message that the thing actually started...
        System.out.println("[Server checker]Starting up");
        // Displaying a message
        System.out.println("Server Ip:");
        // Getting the users input and assigning it to the ip string
        String ip = scan.nextLine();
        // Getting the users input, converting it to an integer from a string, and assigning that to the port int
        System.out.println("Server Port:");
        int port = Integer.parseInt(scan.nextLine());
        // Saying whether or not the server is online.  It uses the checkServer method to do so.
        System.out.println(ip+":"+port+" is "+checkServer(ip, port));
        // I put all of this in a while statement so the user doesen't have to restart the program every time they want to check a server
        while(goAgain()) {
            // Displaying a message
            System.out.println("Server Ip:");
            // Getting the users input and assigning it to the ip string
            ip = scan.nextLine();
            // Getting the users input, converting it to an integer from a string, and assigning that to the port int
            System.out.println("Server Port:");
            port = Integer.parseInt(scan.nextLine());
            // Saying whether or not the server is online.  It uses the checkServer method to do so.
            System.out.println(ip+":"+port+" is "+checkServer(ip, port));
        }
    }
    /**
     * Checks if the server is online or not.
     */
    public static String checkServer(String ip, int port) {
        // Attempting to connect to the server
        try {
            // We made our connection.  The server is online.
            Socket sock = new Socket(ip, port);
            // We don't want to leave the connection open do we?
            sock.close();
            // Make the string checkServer hold the value online.
            return "online";
        } catch(Exception e) {
            // We could not make the connection.  Either the server is offline, or we can't connect for some reason :(
            return "offline";
        }
    }
    /**
     * Checks to see if the user would like to check another server
     */
    public static boolean goAgain() {
        // Displaying instruction methods.
        System.out.println("Would you like to check another?");
        System.out.println("Type y for yes, n for no.");
        // Getting the user's input and assigning it to string choice
        String choice = scan.nextLine();
        // if there choice was yes (y)
        if(choice.equalsIgnoreCase("y"))
            // we go again
            return true;
        else
            // if not, the program ends.
            return false;
    }
}

« Last Edit: January 28, 2009, 02:11:31 pm by Mdog »

Offline Qub1

  • SMF For Free Full Member
  • *
  • Posts: 149
    • View Profile
    • QUB1

  • Total Badges: 16
    Badges: (View All)
    Topic Starter Combination Level 3 Level 2 Level 1
Re: Simple server checker
« Reply #1 on: March 12, 2009, 07:30:11 am »
This would be done with batch files in cmd, right? Maybe try making the option to open the site in a new window. Just add this
Code: [Select]
start iexplore.exe www.url.com or this
Code: [Select]
start firefox.exe www.url.com

Offline ipwxy

  • SMF For Free Newbie
  • *
  • Posts: 9
    • View Profile

  • Total Badges: 6
    Badges: (View All)
    Windows User Topic Starter Level 1 First Post Second year Anniversary
Re: Simple server checker
« Reply #2 on: June 09, 2009, 08:59:01 pm »
This would be done with batch files in cmd, right? Maybe try making the option to open the site in a new window. Just add this
Code: [Select]
start iexplore.exe www.url.com or this
Code: [Select]
start firefox.exe www.url.com
Yes, you have to put it in a java file, then compile it by making a batch file, and putting:

Code: [Select]
@echo
javac nameHere.java
pause

Then run it using a run batch file.
I also like how he puts lots of notes in his code. Very organized. :)

 

Related Topics

  Subject / Started by Replies Last post
24 Replies
4454 Views
Last post September 08, 2006, 05:25:28 pm
by Crasy
1 Replies
1229 Views
Last post May 08, 2007, 02:42:30 pm
by Crasy
7 Replies
1814 Views
Last post December 10, 2007, 01:42:44 am
by Celebrus
4 Replies
1837 Views
Last post March 02, 2008, 06:27:42 am
by MyrtleGail
4 Replies
1466 Views
Last post January 31, 2009, 10:23:19 pm
by Jade