| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 311 人关注过本帖
标题:使用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: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:32
帖 子:409
专家分:1797
注 册: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.048517 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved