Hey folks, recently I wrote a small application in java where it generates ip addresses in a 192.168.x.x block, and then proceeds to ping them to see if the address is available or not.

The main issue with it right now is that it's extremely slow when I tested it in a network, and so sometimes it takes 10 minutes just to finish up an address beginning with 192.168.1.x, before proceeding with the following block.

I'm still unclear on multithreading, so I was wondering if anyone here can show me how to implement it into my code.

Here's my code:

import java.io.*;
import java.net.*;
import java.net.InetAddress;

public class pingBlock
{
	public static void main(String args[])
	{
		int oct1, oct2;
		
		for(oct1 = 1; oct1 <=255; oct1++)
		{
			for(oct2 = 1; oct2 <=255; oct2++)
			try
		{
			InetAddress address = InetAddress.getByName(String.format("192.168.%d.%d", oct1, oct2));
			boolean reachable = address.isReachable(1000);

			System.out.println("Addr: " + address.getHostAddress());
       		System.out.println("Reach: " + address.isReachable(3000));
    	}
    		catch (UnknownHostException e)
    		{
    			System.out.println("unable to look up IP Address block");
    		}
			catch (IOException e)
			{
				System.out.println("unable to look up IP Address block");
			}
		}
	
	}
}

Recommended Answers

All 4 Replies

How long does it take if you manually ping an address?
The timeout is (1+3) seconds, so (255*4)/60 = 16+ minutes

Add some debugging code to printout the System currentTimeMillis to see how long each "ping" takes.

ok, how do I do that? I'm afraid I don't really know how to add the debugging code part.

Its very simple. Add the following at various points in the code where you want to see the values of variables.
Use System.outprintln("var=" + var); // show the value of var

For timings use something like the following:

long beginTest = System.currentTimeMillis();
           .... body of testing code here
      System.out.println("test took=" + (System.currentTimeMillis() - beginTest));   //test took=9969

actually, I'm still not clear on how to do this, on how to add the variables and at where should I place the timing for the code.

I haven't done this before, so I'm still unclear how to get it done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.