| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
买学问 - 大牛一对一辅导,有问必答买学问 - 专业的付费知识问答平台
共有 434 人关注过本帖
标题:使用InetAddress类的方法获取http://www.hunnu.edu.cn/的主机的IP地址;获取 ...
只看楼主 加入收藏
云团
Rank: 2
等 级:论坛游民
帖 子:34
专家分:34
注 册:2017-1-9
结帖率:50%
  已结贴   问题点数:20  回复次数:2   
使用InetAddress类的方法获取http://www.hunnu.edu.cn/的主机的IP地址;获取本地机的名称和IP地址。
大佬帮忙看看哪里有问题!!!
程序代码:
import java.net.*;

public class InetAdressTest {
    public static void main(String[] args){
        InetAddress address;
        try {
            address = InetAddress.getByName("http://www.hunnu.edu.cn/");
            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();
        }
    }
}


附件: 您没有浏览附件的权限,请 登录注册
2019-01-11 15:35
liumq235
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:17
注 册:2017-2-2
  得分:10 
package test;

import java.net.InetAddress;
import java.net.UnknownHostException;

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());
     }
            
}
2019-01-17 23:16
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:421
专家分:1861
注 册:2018-2-6
  得分:10 
程序代码:
import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAdressTest {
    public static void main(String[] args){
        InetAddress address;
        try {
            address = InetAddress.getByName("www.hunnu.edu.cn");
            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去掉就可以了

假如人生没有梦想,和咸鱼有什么区别!
2019-01-18 11:33







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028152 second(s), 9 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved