Open In App

Pinging an IP Address in Java | Set 1

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

PING stands for Packet InterNet Groper in the computer networking field. It’s a computer network administration software used to test the reachability of a host on an Internet Protocol (IP) network. It measures the round-trip time for messages sent from the originating host to a destination computer that are echoed back to the source. Ping operates by sending Internet Control Message Protocol (ICMP/ICMP6 ) Echo Request packets to the target host and waiting for an ICMP Echo Reply. The program reports errors, packet loss, and a statistical summary of the results.

Understanding ICMP

The Internet Control Message Protocol (ICMP) supports protocol in the Internet Protocol suite. It is used by network devices like routers to send error messages and operational information indicating whether a service request is available or not, or that a host or router could not be reached. ICMP differs from transport protocols such as TCP and UDP in that it is not typically used to exchange data between systems. ICMP is not supported in Java, and ping in Java relies on ICMP. We can’t simply ping in Java as it relies on ICMP, which is sadly not supported in Java. This Java Program pings an IP address in Java using the InetAddress class. It is successful in the case of the Local Host, but for other hosts, this program shows that the host is unreachable.

Java Program to Ping an IP Address

In Java, we can use the InetAddress class to check if an IP address is reachable.

Example:

JAVA
// Java Program to Ping an IP address
import java.io.*;
import java.net.*;

class Geeks
{
  // Sends ping request to a provided IP address
  public static void sendPingRequest(String ipAddress)
              throws UnknownHostException, IOException
  {
    InetAddress geek = InetAddress.getByName(ipAddress);
    System.out.println("Sending Ping Request to " + ipAddress);
    if (geek.isReachable(5000))
      System.out.println("Host is reachable");
    else
      System.out.println("Sorry! We can't reach to this host");
  }

  // Driver code
  public static void main(String[] args)
          throws UnknownHostException, IOException
  {
    String ipAddress = "127.0.0.1";
    sendPingRequest(ipAddress);

    ipAddress = "133.192.31.42";
    sendPingRequest(ipAddress);

    ipAddress = "145.154.42.58";
    sendPingRequest(ipAddress);
  }
}

Output
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 133.192.31.42
Sorry! We can't reach to this host
Sending Ping Request to 145.154.42.58
Sorry! We can't reach to this host

Limitations and Platform-Specific Behavior

  • It is very important to understand the InetAddress.isReachable() does not always use ICMP Echo Requests. This method can use other protcols such as TCP to check if the host is reachable or not, which is different from ICMP ping, it simply means the result of the program can vary based on the system.
  • One more important thing is some system block ICMP packets for security reasons, so it might be possible if a host is reachable, the program might say that it is not avaiable. External firewalls or routers also block ICMP packets, which can cause method failure.


Next Article
Article Tags :
Practice Tags :

Similar Reads