Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to Find machines connected to a network

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to Find machines connected to a network

    This code will effectively ping an IP range looking for machines on a network.

    It isn't the fastest method but works none the less.

    import java.io.IOException;
    import java.net.InetAddress;
     
    public class NetworkPing {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) throws IOException {
     
            InetAddress localhost = InetAddress.getLocalHost();
            // this code assumes IPv4 is used
            byte[] ip = localhost.getAddress();
     
            for (int i = 1; i <= 254; i++)
            {
                ip[3] = (byte)i;
                InetAddress address = InetAddress.getByAddress(ip);
            if (address.isReachable(1000))
            {
                System.out.println(address + " machine is turned on and can be pinged");
            }
            else if (!address.getHostAddress().equals(address.getHostName()))
            {
                System.out.println(address + " machine is known in a DNS lookup");
            }
            else
            {
                System.out.println(address + " the host address and host name are equal, meaning the host name could not be resolved");
            }
            }
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following User Says Thank You to JavaPF For This Useful Post:

    Hallowed (April 8th, 2011)


  3. #2
    Junior Member friez's Avatar
    Join Date
    Jun 2011
    Location
    General Santos City
    Posts
    2
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Find machines connected to a network

    great program sir;
    but will you please explain this part of code
    InetAddress localhost = InetAddress.getLocalHost();
    // this code assumes IPv4 is used
    byte[] ip = localhost.getAddress();

    for (int i = 1; i <= 254; i++)
    {
    ip[3] = (byte)i;
    InetAddress address = InetAddress.getByAddress(ip);
    if (address.isReachable(1000))
    for me. if you don't mind. I'm just novice with Java.
    Java means

Similar Threads

  1. Replies: 6
    Last Post: July 16th, 2013, 04:21 AM
  2. calling a java a class connected to derby from a batch file
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 03:01 PM
  3. blocking a network connection
    By BraveProgrammer in forum Java Networking
    Replies: 4
    Last Post: September 2nd, 2010, 12:08 PM
  4. Network problem
    By subash in forum Java Networking
    Replies: 0
    Last Post: March 9th, 2010, 07:07 AM
  5. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 17th, 2009, 11:07 PM