环境:JDK1.3
怎样使用JDK1.3提供的API读取于网卡绑定的IP地址,而不是/etc/hosts表中的内容?
谢谢!

解决方案 »

  1.   

    我在1.42下能运行
    import java.net.*;
    public class getIP{
    public  void printIP(){
    try{
    InetAddress add=InetAddress.getLocalHost();
    System.out.println(add.getHostName()+": "+add.getHostAddress());
    }catch(Exception e)
    {
    System.out.print(e.getMessage());
    }
    }
    public static void main(String []argv)
    {

    getIP ip=new getIP();
    ip.printIP();
    }
    }
      

  2.   

    //iplabel.java
    import java.net.*;
    import java.awt.*;
    import javax.swing.*; 
    public class iplabel
    {
    JFrame frame;
    JLabel label1,label2;
    JPanel panel;
    String ip,address; public void getip()
    {
    try
    {
    InetAddress addr = InetAddress.getLocalHost();
    ip=addr.getHostAddress().toString();    //获得本机IP
    address=addr.getHostName().toString();  //获得本机名称 //String hostname = InetAddress.getByName("199.1.32.90").getHostName()
    //System.out.println("addr=:"+String.valueOf(addr));
    }
    catch(Exception e)
    {
    System.out.println("Bad IP Address!"+e);

    } public void showframe()
    {
    frame=new JFrame("我的IP");
    label1=new JLabel("this my ip");
    label1.setText(ip);
    label2=new JLabel("this my address");
    label2.setText(address);
    panel=new JPanel();
    panel.add(label1);
    panel.add(label2);
    frame.getContentPane().add(panel); frame.setSize(400,300);
    frame.setVisible(true);
    } public static void main(String agrs[])
    {
    iplabel myip=new iplabel();
    myip.getip();
    myip.showframe(); 
    }
    }
      

  3.   

    老大,我说的是jdk1.3啊,而且调用InetAddress.getLocalHost()读的是/etc/hosts文件中的内容,hosts中的IP与实际IP不一致的话,就会读错
      

  4.   

    方法一:jni。编写dll。在java中调用。。
    方法二:通过Runtime执行ipconfig /all
    Process p = Runtime.getRuntime().exec("ipconfig /all");
    InputStream is = p.getInputStream();将is转成字符串。分析麻烦了一点。呵呵
      

  5.   

    谁说java不可以,你们用错类了。可以查找网络接口。然后获得接口上的ip地址
    //-----------------------ipget.java-----------------------
    import java.net.InetAddress;
    import java.net.SocketException;import java.net.UnknownHostException;import java.net.NetworkInterface;
    import java.util.Enumeration;
    public class ipget { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String addipmsg;
    try {
    Enumeration eth = NetworkInterface.getNetworkInterfaces();

    for (int i=0;eth.hasMoreElements() ;) {
    if (eth.nextElement().equals("name:lo"))
    continue;


    addipmsg=eth.nextElement().toString();

    i=addipmsg.indexOf("/",0)+1;

    System.out.println(addipmsg.substring(5,8));
    System.out.println(addipmsg.substring(i));
          
         }

    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }}
      

  6.   

    import java.net.*;//引用InetAddress类所在的包
    public class MyIPAddress
    {
    public static void main(String args[])
    {
    try{
    if(args.length==1)
    {//调用InetAddress类的静态方法,利用主机名创建对象
     InetAddress ipa=InetAddress.getByName(args[0]);
     System.out.println("Host name:"+ipa.getHostName());//获取主机名
     System.out.println("Host IP Address:"+ipa.getHostAddress());//获取主机IP地址
     System.out.println("Local Host:"+InetAddress.getLocalHost());
    }
    else
     System.out.println("请输入一个主机名作为命令行参数");
    }
    catch(UnknownHostException e)//创建InetAddress对象可能引发的异常
    {
    System.out.println(e.toString());
    }
    }//end of main()
    }