Friday 9 October 2015

NetworkInterface : network interface card

Network interface is the point of interconnection between a computer and a network. A network interface is generally a network interface card (NIC).

Note:
A NIC is need not be a hardware, it can be software also,

Network Interface Card
A network interface card (NIC) is a circuit board that is installed in a computer so that it can be connected to a network.

Using NetworkInterface, you can specify which NIC to use for a particular network activity. By using NetworkInterface class, you can get the information about configured NICs in your machine.

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Main {
  public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface
        .getNetworkInterfaces();

    while (interfaces.hasMoreElements()) {
      System.out.println(interfaces.nextElement().getName());

    }
  }
}


Sample Output
en3
en0
lo0

How to getNetworkInterface Object
NetworkInterface class provides following factory methods, to get an instance of NetworkInterface class.

static NetworkInterface        getByIndex(int index)
static NetworkInterface        getByInetAddress(InetAddress addr)
static NetworkInterface        getByName(String name)
static Enumeration<NetworkInterface> getNetworkInterfaces()

a. static NetworkInterface getByIndex(int index)
Get a network interface given its index or null if there is no interface with such an index on the system.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class Main {
  public static void main(String args[]) throws SocketException {      
    NetworkInterface nif = NetworkInterface.getByIndex(1);
    
    System.out.println(nif.getDisplayName());
    
    Enumeration<InetAddress> addresses = nif.getInetAddresses();
    List<InetAddress> addrList = Collections.list(addresses);
    
    for(InetAddress addr : addrList){
      System.out.println(addr);
    }
  }
}


b. static NetworkInterface getByInetAddress(InetAddress addr)

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class Main {
  public static void main(String args[]) throws SocketException,
      UnknownHostException {
    InetAddress local = InetAddress.getByName("127.0.0.1");
    NetworkInterface nif = NetworkInterface.getByInetAddress(local);

    System.out.println(nif.getDisplayName());

    Enumeration<InetAddress> addresses = nif.getInetAddresses();
    List<InetAddress> addrList = Collections.list(addresses);

    for (InetAddress addr : addrList) {
      System.out.println(addr);
    }
  }
}


c. static NetworkInterface getByName(String name)

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class Main {
  public static void main(String args[]) throws SocketException {      
    NetworkInterface nif = NetworkInterface.getByName("en3");
    
    System.out.println(nif.getDisplayName());
    
    Enumeration<InetAddress> addresses = nif.getInetAddresses();
    List<InetAddress> addrList = Collections.list(addresses);
    
    for(InetAddress addr : addrList){
      System.out.println(addr);
    }
  }
}


d. static Enumeration<NetworkInterface> getNetworkInterfaces()

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class Main {
  public static void main(String args[]) throws SocketException,
      UnknownHostException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface
        .getNetworkInterfaces();

    while (interfaces.hasMoreElements()) {
      NetworkInterface nif = interfaces.nextElement();

      System.out.println(nif.getDisplayName());

      Enumeration<InetAddress> addresses = nif.getInetAddresses();
      List<InetAddress> addrList = Collections.list(addresses);

      for (InetAddress addr : addrList) {
        System.out.println(addr);
      }
    }
  }
}


Referred Articles




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment