Java Tutorial
//Returns the IP address of Local machine. static InetAddress.getLocalHost() throws UnknownHostException //Returns IP address of given host name static InetAddress.getByName(String host) throws UnknownHostException //Returns an array of IP addresses of particular host name. static InetAddress.getAllByName(String host) throws UnknownHostException static InetAddress.getByAddress(byte addr[]) throws UnknownHostExceptionTo get address of local machine
import java.net.*; class InetAddressDemo { public static void main(String args[]) throws UnknownHostException { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); } }To get address from host name.
import java.net.*; class InetAddressDemo { public static void main(String args[]) throws UnknownHostException { InetAddress address = InetAddress.getByName("www.google.com"); System.out.println(address); } }To get array of host address associated to domain name.
import java.net.*; class InetAddressDemo { public static void main(String args[]) throws UnknownHostException { InetAddress address[] = InetAddress.getAllByName("www.google.com"); for(int i=0; i<address.length; i++) System.out.println(address[i]); } }
import java.net.*; class InetAddressDemo { public static void main(String args[]) throws UnknownHostException { byte[] ipAddr = new byte[] {(byte)172,(byte)217,(byte)26,(byte)196}; InetAddress address = InetAddress.getByAddress(ipAddr); System.out.println(address); } }
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page