我记得好像是可以的,以前我好像用
        InetAddress ipaddr = InetAddress.getByName("www.sina.com.cn");
        String hostname = ipaddr.getHostName();
        InetAddress [] ipaddrs = InetAddress.getAllByName(hostname);
就能取到多个地址

解决方案 »

  1.   

    能    for(int i=0;i<ipaddrs.length;++i){
          System.out.println( ipaddrs[i].getHostAddress());
          
        }
      

  2.   

    InetAddress ipaddr = InetAddress.getByName("www.sina.com.cn");
    String hostname = ipaddr.getHostName();
    InetAddress [] ipaddrs = InetAddress.getAllByName(hostname); 上面的代码是不是取到的是通过域名解析得到的IP地址,如果要得本地多个网卡的IP,一定能行么?
      

  3.   

    "上面的代码是不是取到的是通过域名解析得到的IP地址"
    我同意你的观点,
    继续关注中。一个域名可以被解析到多个IP吗?是轮询的呢,还是并发地把对一域名的服务请求分发到多个不同IP的服务器上?请高手赐教啊。不胜感谢
      

  4.   

    import java.awt.* ; 
    import java.net.* ; 
    import java.awt.event.* ; 
    import javax.swing.* ;  
    public class GUIIp extends JPanel 

    JTextField texthost ; 
    JLabel labelip ; 
    String ipstring ; 
    JButton displaybutton ; 
    InetAddress IpAddress = null ;   
    GUIIp() 
    {  
    labelip = new JLabel("ip address") ; 
    texthost = new JTextField("please enter a host name.", 15) ; 
    //texthost.selectAll() ; 
    displaybutton = new JButton("display ip address") ; 
    setLayout(new GridLayout(0,1)) ; 
    add(texthost) ; 
    add(displaybutton) ; 
    add(labelip) ; 
    displaybutton.addActionListener(new ActionListener() 

    public void actionPerformed(ActionEvent e) 

    ipstring = texthost.getText().trim() ; 
    texthost.selectAll() ; 
    try 

    IpAddress = InetAddress.getByName(ipstring) ;  labelip.setText(IpAddress.getHostAddress()) ; 
    }
    catch(UnknownHostException eip) 

    System.out.println(eip.toString()) ; 


    }) ; 
    }  
    public static void main(String[] args) 

    JFrame frame = new JFrame("Display ip address") ; 
    frame.addWindowListener(new CloseWindow()) ; 
    frame.getContentPane().add(new GUIIp()) ; 
    frame.pack() ; 
    frame.setVisible(true) ; 

    }  
    class CloseWindow extends WindowAdapter 

    public void windowClosing(WindowEvent e) 

    System.exit(0) ; 

    }