A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© tangxiaobo1991 中级黑马   /  2014-4-1 10:08  /  1076 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

为什么我用下面的方法获得的主机名和IP地址始终是localhost和127.0.0.1,而不是我的真实IP192.168.1.25
package nettest;
import java.net.*;
public class NetTest1 {

        public static void main(String[] args) throws Exception{
                // TODO Auto-generated method stub
                InetAddress i=InetAddress.getLocalHost();
                System.out.println(i.toString());
                System.out.println(i.getHostAddress());
                System.out.println(i.getHostName());
               
        }

}


评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

2 个回复

倒序浏览
StringBuilder IFCONFIG=new StringBuilder();  
                   try {  
                       for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
                           NetworkInterface intf = en.nextElement();  
                           for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
                               InetAddress inetAddress = enumIpAddr.nextElement();  
                               if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {  
                               IFCONFIG.append(inetAddress.getHostAddress().toString()+"\n");  
                               }  
                  
                           }  
                       }  
                   } catch (SocketException ex) {  
                   }  
                   System.out.println(IFCONFIG);
你可以试试这段代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
因为你没有连接
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马