I have searched a lot but i could not find any way to convert inetaddress type to string(may be my searching is not that good :S).I have to convert it into string type because i have to display the result in textarea(gui component) which requires string type.So can anyone how is this possible??
InetAddress class provides methods to get the IP of any host name for example www.javatpoint.com, www.google.com, www.facebook.com, etc. An IP address is represented by 32-bit or 128-bit unsigned number. An instance of InetAddress represents the IP address with its corresponding host name.
The getByName() method of InetAddress class determines the IP address of a host from the given host's name. If the host name is null, then an InetAddress representing an address of the loopback interface is returned.
getHostName() Gets the host name for this IP address. static InetAddress. getLocalHost() Returns the address of the local host.
The toString() method of Java InetAddress class converts this IP address to a String . The string returned is of the form: hostname/ literal IP address. If the host name is unresolved, no reserve name service lookup is performed.
Java API for InetAddress class has good methods for your needs, I see. Try these methods. You can check out other property getters of InetAddress for your further requirements.
public static void main ( String [] args ) throws UnknownHostException
{
// Instantiate your own InetAddress object.
InetAddress address = InetAddress.getLocalHost();
String hostIP = address.getHostAddress() ;
String hostName = address.getHostName();
System.out.println( "IP: " + hostIP + "\n" + "Name: " + hostName);
}
Have you tried to InetAddress.toString() method?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With