Java Network Interface Get pingFromInterface(String name)

Here you can find the source of pingFromInterface(String name)

Description

ping From Interface

License

Open Source License

Declaration

public static boolean pingFromInterface(String name) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.*;

import java.util.Enumeration;
import static java.lang.System.out;

public class Main {
    public static final String IPv6_ONLY_SERVER_IP = "2001::7";
    public static final int IPv6_ONLY_SERVER_PORT = 22;

    public static boolean pingFromInterface(String name) throws IOException {
        out.println("Pinging from interface " + name);

        boolean connected = false;

        NetworkInterface networkInterface = NetworkInterface.getByName(name);
        if (networkInterface == null) {
            out.println("No network interface for " + name);
            return false;
        }/*from www  . j a  v a 2 s  .co  m*/

        Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

        while (inetAddresses.hasMoreElements()) {
            InetAddress localAddr = inetAddresses.nextElement();
            out.println("Using address " + localAddr);

            connected |= pingFromLocalAddress(localAddr);
        }
        return connected;
    }

    public static boolean pingFromLocalAddress(InetAddress localAddr) throws IOException {
        boolean connected = false;
        Socket socket = null;
        try {
            socket = new Socket(IPv6_ONLY_SERVER_IP, IPv6_ONLY_SERVER_PORT, localAddr, 0);
            out.println("Created!");
            connected = true;
        } catch (IOException e) {
            out.println("Failed to connect");
            e.printStackTrace(out);
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
        return connected;
    }
}

Related

  1. getPublicInterface()
  2. getUsualNetworkInterfaces()
  3. isActive(String niName)
  4. isNixMashPC()
  5. parseInterfaceList(String s)
  6. print(Collection objs)
  7. printValidInterfaces(PrintWriter pout)
  8. toNI(String niName)