ping ip address using socket programing in java code example

Example: ping ip address using socket programing in java

/* ............... START ............... */
                
import java.net.*;

public class JavaPingExample {
	public static void main(String[] args) {

		try {
			String ipAddress = "127.0.0.1";
			InetAddress inet = InetAddress.getByName(ipAddress);
			System.out.println("Sending Ping Request to " + ipAddress);
			if (inet.isReachable(5000)) {
				System.out.println(ipAddress + " is reachable.");
			} else {
				System.out.println(ipAddress + " NOT reachable.");
			}
		} catch (Exception e) {
			System.out.println("Exception:" + e.getMessage());
		}
	}
}

                /* ............... END ............... */

Tags:

Java Example