Académique Documents
Professionnel Documents
Culture Documents
import java.net.*; public class Ipaddress { public static void main(String args[]){ try{ InetAddress local= InetAddress.getLocalHost(); System.out.println ("Local IP Address is : " + local); } catch (UnknownHostException e) { System.err.println ("Can't detect IP Address : " + e); } } }
Retrieve the information about the local host name using the getHostName() method. Create a class "IPReverse". Call the InetAddress class and create a class object and pass the IP address in InetAddress.getByName() method. Now retrieve the Host name of the IP address what we are pass in getByName() method using the getHostName() method.
import java.net.*; public class IPReverseTest { public static void main (String[] args) { try { InetAddress ia = InetAddress.getByName("192.168.10.105"); System.out.println(ia.getHostName()); } catch (Exception ex) { System.err.println("does not fine hostname"); } }}
import java.net.*; public class URLDemo{ public static void main(String args[])throws MalformedURLException{ URL hp = new URL("http://www.javajazzup.com");
System.out.println("Protocal:" + hp.getProtocol()); System.out.println("Port:" + hp.getPort()); System.out.println("Host:" + hp.getHost()); System.out.println("File:" + hp.getFile()); System.out.println("Ext:" + hp.toExternalForm()); }}
5.ReadIPAddress
Method to retrieve the IPAddress of the local system in a very simple example. For this, we
use getByName() method of InetAddress class to create a object that contains IP address for the given host name. Using this object we can get IP address using method getHostAddress(). Now we can use the print statement to print this value. If the user enters the host name by keyboard then IP address of local machine will be displayed on console. getHostAddress(): This method of InetAddress class returns an IPAddress of local system.
import java.net.*; import java.io.*; public class ReadIPAddress{ public static void main(String[] args )throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the host name: "); String hostname = input.readLine(); try { InetAddress inetadd = InetAddress.getByName(hostname); System.out.println("IP address is: " + inetadd.getHostAddress()); } catch(UnknownHostException e ){ System.out.println("Could not find IP address for: " + hostname); } }}
7. URLReadFile
import java.net.*; import java.io.*; public class URLReadFile{ public static void main(String[] args) throws IOException{
try{ URL url = new URL ("file:///C:/Documents%20and%20Settings/comp23/Desktop/AnimationLine.html"); BufferedReader buff = new BufferedReader(new InputStreamReader(url.openStream())); String str; while((str = buff.readLine()) != null) { System.out.println("\n"); System.out.println(str); } buff.close(); } catch(IOException e){ System.err.println(e); } } }