Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

Help With Pinging A Website In Java.....? :3

Discussion in 'General Computing' started by blackneos940, May 11, 2018.

  1. blackneos940

    blackneos940 Active Member

    Joined:
    May 16, 2017
    Messages:
    129
    Likes Received:
    76
    Hello everyone..... :3 As I sit here sipping Coffee, I'm trying to get this Java Program I got over at "https://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-an-http-url-for-availability" to work..... First, here's the actual Code..... :3

    Code:
    import java.io.*;
    
    import java.net.Socket;
    
    public class Ping_Website
    
    {
      public static boolean ping_host(String host, int port, int timeout)
    
      {
        try(Socket socket = new Socket())
    
        {
          socket.connect(new InetSocketAddress(host, port), timeout);
    
          return true;
    
        }
    
        catch(IOException e)
    
        {
          return false; //Either a Timeout or unreachable or failed DNS lookup
    
        }
    
      }
    
    }
    As you can see, I made some changes to the actual Code (so it would Compile, or at least come close to doing so)..... :) Any help.....? :3 Does anyone know how to Program in Java.....? :3 FYI (if it matters), I have Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10) installed (this is what "java --version" reveals in Guake)..... :3 Anywho, thanks for any help guys..... :3 I REALLY appreciate it..... :3

    EDIT: When I type "javac Ping_Website.java", it outputs:



    Code:
    Ping_Website.java:14: error: cannot find symbol
          socket.connect(new InetSocketAddress(host, port), timeout);
                             ^
      symbol:   class InetSocketAddress
      location: class Ping_Website
    1 error
    Anywho, on to you, fellow Penguinites!!..... :3
     
  2. JasKinasis

    JasKinasis Well-Known Member

    Joined:
    Apr 25, 2017
    Messages:
    302
    Likes Received:
    573
    I can see one obvious thing:
    - You haven't imported the InetSocketAddress class:
    Code:
    import java.net.InetSocketAddress;
    
    That should enable your java class to compile cleanly, but your java class currently only contains the static ping_host function. It won't do actually do anything.

    In order to use the ping_host function, you would have to include a main function somewhere that uses the ping_host function.
    e.g.
    You could include the following main function somewhere in Ping_Website.java:
    Code:
    public static void main(String args[])
    {
        if(ping_host("google.com", 443, 60))
            System.out.println("google.com is available on port 443 (https)!");
        else
            System.out.println("google.com is not available on port 443 (https)!");
    }
    
    NOTE: Make sure you add the function somewhere inside the class definition for Ping_Website.
    e.g. After the closing brace } of the ping_host function, but before the closing brace } for the class declaration/definition.

    The above main() function uses the ping_host function to check whether google.com can be reached on port 443 (for https://).

    Alternatively, you could create a new java class (in a new .java file) to house your main function.
    e.g.
    TestPing.java:
    Code:
    public class TestPing
    {
      public static void main(String args[])
      {
         if(Ping_Website.ping_host("google.com", 443, 60))
           System.out.println("google.com is up!");
         else
           System.out.println("google.com is down!");
      }
    }
    
    Choose one method or the other - depending on what you want to do.
    If you want the main function to be a part of your Ping_Website class, then put it in Ping_Website.java. Otherwise, if Ping_Website is intended to be a library, then put your main function in a different class - e.g. TestPing.java.

    If you decide to put main into a different .java file:
    As long as the new .java file is in the same directory as Ping_Website.java, you shouldn't need to import anything.
    But any time you use the ping_host function outside of Ping_Website.java, you will have to call it like this:
    Code:
    Ping_Website.ping_host
    Otherwise java won't know where to find the ping_host function.

    [EDIT]
    Just in case anything is unclear, I've attached a .zip containing two sub-directories. One showing the all-in-one file approach and one showing the separate classes/files approach. There are java files and simple build scripts in there. Just run each directories build.sh to compile the .class files.

    After building the .class files. In the 'allinone' directory, run:
    Code:
    java Ping_Website
    
    And in the 'separate' directory run:
    Code:
    java TestPing
    
    [/EDIT]
     

    Attached Files:

    #2 JasKinasis, May 12, 2018
    Last edited: May 12, 2018
    blackneos940 and atanere like this.
  3. blackneos940

    blackneos940 Active Member

    Joined:
    May 16, 2017
    Messages:
    129
    Likes Received:
    76
    Hey there good sir!..... :3 Sorry I took so long in replying..... :( Anywho, I edited the Java File I had on my Server where I and some friends log into to, well, screw around and Program on..... :3 It didn't Compile, but 'lo and behold, my buddy Jas had written some Java Files for me to study!..... :3 I really appreciate this, good sir..... :3 I'm trying to get better at different Languages, not JUST C..... :3 But I will always love C as my first major study of a Programming Language....... :"3
     
    JasKinasis likes this.

Share This Page