import java.net.*;
public class getName{
public static void main(String args[]) throws Exception{
InetAddress host = null ;
host = InetAddress.getLocalHost();
System.out.println("Nama komputer Anda :" + host.getHostName());
}
}
Program ini bertujuan untuk mendapatkan nama komputer dari Alamat IP.
import java.net.*;
public class IPtoName{
public static void main(String args[]){
if (args.length == 0){
System.out.println("Pemakaian : java IPtoName
System.exit(0);
}
String host = args[0] ;
InetAddress address = null ;
try{
address = InetAddress.getByName(host);
}catch(UnknownHostException e){
System.out.println("invalid IP");
System.exit(0);
}
System.out.println(address.getHostName());
}
}
Jalankan dengan argumen IP komputer lokal dan komputer lain.
$ javac IPtoName.java
$ java IPtoName
$ java IPtoName
$ java IPtoName
Program di bawah ini, masukkan misal www.detik.com maka akan ditampilkan Alamat IP dari www.detik.com. Masukkan host name : java.sun.com, berapakah Alamat IPnya?
import java.net.*;
import java.io.*;
public class IPFinder{
public static void main(String args[]) throws IOException{
String host;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter host name : ");
host = input.readLine();
try{
InetAddress address = InetAddress.getByName(host);
System.out.println("IP Address : " + address);
}catch(UnknownHostException e){
System.out.println("Could not find " + host);
}
}
}
import java.net.*;
import java.io.*;
public class IPFinder{
public static void main(String args[]) throws IOException{
String host;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter host name : ");
host = input.readLine();
try{
InetAddress address = InetAddress.getByName(host);
System.out.println("IP Address : " + address);
}catch(UnknownHostException e){
System.out.println("Could not find " + host);
}
}
}
Tidak ada komentar:
Posting Komentar