IP:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpLoc {
public static void main(String[] args) throws UnknownHostException{
//本机
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress()); //
System.out.println(addr.getHostName());
System.out.println();
//根据域名
addr = InetAddress.getByName("www.baidu.com");
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
System.out.println();
//根据IP
addr = InetAddress.getByName("61.135.169.121");
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
}
}
端口:
URL: