I feel stupid for actually asking this but how do I ping a IP address in Java?

I have this code

			// Google.com
			if (InetAddress.getByAddress("".getBytes()).isReachable(1000)==true)
                                    //Boolean variable named network
				network=true; //Ping works
				network=false; //Ping doesnt work
		catch (UnknownHostException e3) 
			System.out.println("ERROR: DNS server error " + e3.getLocalizedMessage());
		catch (IOException e3) 
			System.out.println("ERROR: Some other error " + e3.getLocalizedMessage());

The reason I use a IP address instead of a more common host name is because I have to also ping a internal address which does not have a host name so Ill just do it more universal and just use IP addresses.....

Recommended Answers

All 3 Replies

Try this with variables defined and without the getbytes method for now:

try {             
     InetAddress adr = InetAddress.getByName("");
     System.out.println("Reachable Host: "+adr.isReachable(3000));  
     boolean status = inet.isReachable(3000);  
     System.out.println("Reachable Host:" + status);      
catch (IOException e) {             

Either using the boolean or not.
Hope that helps :)

What is "inet.isReachable(3000);"???

This code

InetAddress adr=InetAddress.getByName(""); //Google.com
			System.out.println("Value 1 " + redexterna);
			adr=InetAddress.getByName(""); //A IP on my local network that does NOT exist
			System.out.println("Value 2 " + redinterna);

Both give me false. Pinging to google works and pinging to that IP doesnt work in CMD....

Just to make sure :)

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.