How to use Java to test network connectivity

Source: Internet
Author: User
Tags socket

Overview

In network programming, sometimes we need to judge the connectivity between two machines, or the network accessibility of a machine to another machine. In a system-level test, we often use the Ping command to do the validation. Although Java provides a richer network programming class library (including URL-based network resource reading at the application level, Socket programming based on TCP/IP layer, and some auxiliary class libraries), there is no direct way to test network connectivity with Ping-like commands. This article will introduce how to realize the network accessibility judgment between two machines in various scenarios through Java API. In the following sections, we'll use some class libraries java.net.InetAddress and Java.net.Socket in Java network programming to illustrate how to simulate Ping commands.

To simply judge the accessibility of two machines

In general, we just need to determine whether from one machine can access (Ping) to another machine, at this time, can simply use the Java class library Java.net.InetAddress class to implement, this class provides two methods to detect whether the remote machine can reach

Boolean isreachable (int timeout)//test address is up to
Boolean isreachable (NetworkInterface netif, int ttl, int timeout)
Test address is up to.

In short, the above method is to construct the InetAddress object by the IP address of the remote machine, then call its Isreachable method to test the network accessibility of the machine and the remote machine. Note that the remote machine may have more than one IP address, so it is possible to iterate over all the scenarios.

Listing 1: Simply judging the accessibility of two machines

void isaddressavailable (String IP) {try{inetaddress address = inetaddress.getbyname (IP);//ping this IP 
      if (address instanceof java.net.Inet4Address) {System.out.println (IP + "are IPv4 address"); 
      }else if (address instanceof java.net.Inet6Address) {System.out.println (IP + "are IPv6 address"); 
      }else{System.out.println (IP + "is unrecongized"); } if (Address.isreachable (5000)) {System.out.println ("success-ping + IP +" with no Interf 
      Ace specified "); 
      }else{System.out.println ("failure-ping" + IP + "with no interface specified"); 
          
      } System.out.println ("\ n-------trying different interfaces--------\ n");    
      enumeration<networkinterface> netinterfaces = Networkinterface.getnetworkinterfaces (); while (Netinterfaces.hasmoreelements ()) {NetworkInterface ni = Netinterfaces.nexTElement ();
     SYSTEM.OUT.PRINTLN ("Checking interface, DisplayName:" + ni.getdisplayname () + ", Name:" + ni.getname ()); 
      if (address.isreachable (NI, 0, 5000)) {System.out.println ("success-ping" + IP); 
      }else{System.out.println ("failure-ping" + IP);    
      } enumeration<inetaddress> ips = Ni.getinetaddresses ();   
      while (Ips.hasmoreelements ()) {System.out.println ("IP:" + ips.nextelement (). gethostaddress ()); 
      } System.out.println ("-------------------------------------------"); 
      }}catch (Exception e) {System.out.println ("error occurs."); 
        E.printstacktrace (); }       
 }

Program output

--------------START-------------- 
    
10.13.20.70 is IPv4 address 
success-ping 10.13.20.70 with no interface Specified 
    
-------trying different interfaces-------- 
    
Checking interface, Displayname:ms TCP loopback interface, Name:lo 
failure-ping 10.13.20.70
ip:127.0.0.1
------------------------------------------- 
Checking Interface, Displayname:intel (r) Centrino (r) advanced-n 6200 AGN- 
Teefer2 Miniport, Name:eth0 
failure-ping 10.13.20.70
ip:9.123.231.40
----------------------------- -------------- 
Checking interface, Displayname:intel (R) 82577LM Gigabit network Connection- 
Teefer2 Miniport, Name:eth1 
success-ping 10.13.20.70
------------------------------------------- 
Checking Interface, Displayname:wan (Ppp/slip) interface, name:ppp0 success-ping 10.13.20.70 ip:10.0.50.189
- ------------------------------------------ 
    
--------------End--------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.