Pinging an IP address in Java | Set 2 (By creating sub-process)
In article Pinging an IP address in Java, we have discussed how to ping an IP address using java.net.InetAddress.isReachable() method. In this post we will discuss how to execute ping command by creating a sub-process.
Prerequisite : ProcessBuilder class , Process class
Below Java program creates a method commands() which take list of command(ping) as a parameter. As we know ProcessBuilder class is used to create operating system processes and ProcessBuilder.start() starts the sub-process which will execute the ping command.
Standard output: PING www.google.com (126.96.36.199): 56 data bytes 64 bytes from 188.8.131.52: icmp_seq=0 ttl=53 time=98.803 ms 64 bytes from 184.108.40.206: icmp_seq=1 ttl=53 time=87.856 ms 64 bytes from 220.127.116.11: icmp_seq=2 ttl=53 time=110.600 ms 64 bytes from 18.104.22.168: icmp_seq=3 ttl=53 time=92.897 ms 64 bytes from 22.214.171.124: icmp_seq=4 ttl=53 time=90.142 ms --- www.google.com ping statistics --- 5 packets transmitted, 5 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 87.856/96.060/110.600/8.140 ms error (if any):
Reading and Understanding the Output: Each line to the destination represents a packet transmission, the time specified in milliseconds at the end is most relevant for testing an internet connection as a higher the number there indicates higher the lag is or a connection problem. If there is no response at all, either the server is down, there is a connection problem, it does not respond to ping requests, or it is very slow to respond.
Output when there is no internet connection: The output of above program become very interesting when our system is not connected to internet
Standard output: error (if any): ping: cannot resolve www.google.com: Unknown host
Understanding the term “Packet loss”: Packet loss is high, you almost certainly have network issues, because packet loss means that data being sent between you and the server is being lost. The reason of pocket loss can be poor internet connection, wi-fi problems, general network problems, a bad connection, a struggling connection, an interfered connection, connection interruptions, or many other potential networking issues.
Related Article : Pinging an IP address in Java
This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.