注册 登录
编程论坛 JAVA论坛

使用InetAddress类的方法获取http://www.hunnu.edu.cn/的主机的IP地址;获取本地机的名称和IP地址。

云团 发布于 2019-01-11 15:35, 2798 次点击
大佬帮忙看看哪里有问题!!!
程序代码:
import *;

public class InetAdressTest {
    public static void main(String[] args){
        InetAddress address;
        try {
            address = InetAddress.getByName("http://www.hunnu.);
            String Hostaddr1 = address.getHostAddress();
            System.out.println("主机IP:" + Hostaddr1);
            address = InetAddress.getLocalHost();
            String HostName = address.getHostName();
            String Hostaddr2 = address.getHostAddress();
            System.out.println("本地主机名:" + HostName);
            System.out.println("本地主机IP:" + Hostaddr2);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

只有本站会员才能查看附件,请 登录

2 回复
#2
liumq2352019-01-17 23:16
package test;

import
import

public class Test {
     public static void main(String[] args) throws UnknownHostException{
         
         //获取指定域名ip
         InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
         System.out.println(inetAddress.getHostAddress());
         //获取本机ip
         InetAddress inetAddress2 = InetAddress.getLocalHost();
         System.out.println(inetAddress2.getHostAddress());
     }
            
}
#3
疯狂的小a2019-01-18 11:33
程序代码:
import import public class InetAdressTest {
    public static void main(String[] args){
        InetAddress address;
        try {
            address = InetAddress.getByName("www.hunnu.);
            String Hostaddr1 = address.getHostAddress();
            System.out.println("主机IP:" + Hostaddr1);
            address = InetAddress.getLocalHost();
            String HostName = address.getHostName();
            String Hostaddr2 = address.getHostAddress();
            System.out.println("本地主机名:" + HostName);
            System.out.println("本地主机IP:" + Hostaddr2);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

把htttp去掉就可以了
1