NetworkInterface.getByInetAddress(InetAddress) in Java

NetworkInterface.getByInetAddress(InetAddress): This method is available in the java.net.NetworkInterface class of Java.

Syntax:

NetworkInterface java.net.NetworkInterface.getByInetAddress(InetAddress addr) throws SocketException

This method takes one argument. Convenience method to search for a network interface that has the specified Internet Protocol (IP) address bound to it.

If the specified IP address is bound to multiple network interfaces it is not defined which network interface is returned.

Parameters: One parameter is required for this method.

addr: The InetAddress to search with.

Returns: A NetworkInterfaceor is null if there is no network interface with the specified IP address.

Throws:

1. SocketException - If an I/O error occurs.

2. NullPointerException - If the specified address is null.

Approach 1: When no exception

Java

package com.NetworkInterface;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class NetworkInterfacegetByInetAddress {
    public static void main(String[] args) throws SocketException, UnknownHostException {

        InetAddress addr = InetAddress.getLocalHost();
        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(addr);

        System.out.println(networkInterface);
    }
}

Output:

name:wlan2 (Intel(R) Dual Band Wireless-AC 3160)


Approach 2: NullPointerException 

Java

package com.NetworkInterface;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class NetworkInterfacegetByInetAddress {
    public static void main(String[] args) throws SocketException, UnknownHostException {

        InetAddress addr = null;
        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(addr);

        System.out.println(networkInterface);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.net.NetworkInterface.getByInetAddress(NetworkInterface.java:315) at com.NetworkInterface.NetworkInterfacegetByInetAddress.main(NetworkInterfacegetByInetAddress.java:12)


Some other methods of NetworkInterface class

equals(Object)This method compares this object against the specified object.

NetworkInterface.getByIndex(int)This method gets a network interface given its index.

NetworkInterface.getByInetAddress(InetAddress)Convenience method to search for a network interface that has the specified Internet Protocol (IP) address bound to it.

NetworkInterface.getByName(String)This method searches for the network interface with the specified name.

getDisplayName()This method gets the display name of this network interface.

getHardwareAddress()This method returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.

getIndex()This method returns the index of this network interface.

getInetAddresses()This method gets an Enumeration with all or a subset of the InetAddresses bound to this network interface.

getInterfaceAddresses()This method gets a List of all or a subset of the InterfaceAddressesof this network interface.

getMTU()This method returns the Maximum Transmission Unit (MTU) of this interface.

getName()This method gets the name of this network interface.

NetworkInterface.getNetworkInterfaces()This method returns an Enumeration of all the interfaces on this machine.

getParent()This method returns the parent NetworkInterface of this interface if this is a sub-interface, or null if it is a physical(non-virtual) interface or has no parent.

getSubInterfaces()This method gets an Enumeration with all the subinterfaces (also known as virtual interfaces) attached to this network interface.

hashCode()This method returns a hash code value for the object.

inetAddresses()This method gets a Stream of all or a subset of the InetAddresses bound to this network interface.

isLoopback()This method returns whether a network interface is a loopback interface.

isPointToPoint()This method returns whether a network interface is a point-to-point interface.

isUp()This method returns whether a network interface is up and running.

isVirtual()This method returns whether this interface is a virtual interface.

NetworkInterface.networkInterfaces()This method returns a Stream of all the interfaces on this machine.

subInterfaces()This method gets a Stream of all subinterfaces attached to this network interface.

supportsMulticast()This method returns whether a network interface supports multicasting or not.

toString()This method returns a string representation of the object. In general, the toString method returns a string that"textually represents" this object.

No comments:

Post a Comment