1. Lấy địa chỉ local host bằng phương thức getLocalHost() của class InetAddress.
2. Lấy địa chỉ IP bằng phương thức getHostAddress().
Chương trình cài đặt.
package simplecodecjava.blogspot.com;
import java.net.InetAddress;
public class GetIPAddress {
public static void main(String[]args) throws Exception{
/*phhương thức static InetAddress getLocalHost() ném ra UnknownHostException,
Phương thức này trả về địa chỉ của local host.*/
InetAddress myIP = InetAddress.getLocalHost();
System.out.print("Địa chỉ IP của máy tính là: ");
System.out.print(myIP.getHostAddress());
}
}
Output:
Địa chỉ IP của máy tính là: 107.113.187.26

0 Comment to "[Java] Lấy IP trong Java"
Post a Comment