2017-03-17 54 views
0

下面的代碼是給以太網地址 但我需要得到Wirless的局域網地址如何讓自己的無線局域網的IP地址使用Java

InetAddress inet; 
    try { 
     inet = InetAddress.getLocalHost(); 
     myIp.setText(inet.getHostAddress()); 
    } catch (UnknownHostException ex) { 
     Logger.getLogger(frontPageController.class.getName()).log(Level.SEVERE, null, ex); 
    } 

但是,什麼是代碼即可獲得無線局域網的IP地址? ?

+1

你做任何研究嗎?你學習了'java.net.NetworkInterface'嗎? –

+0

是java.net.NetwokInterface類將提供所有IP地址的列表,但我只需要Wirless局域網地址@JimGarrison –

+0

再次研究'NetworkInterface'類。有更多的方法可用。您必須能夠確定您想要的API接口(名稱,MAC,IP等)的接口。 API中沒有明確區分無線接口和其他任何接口,因爲在這個級別上它們都是以太網。 –

回答

1

這是一個例子,你如何與java.net.NetworkInterface探索網絡接口:

@Test 
    public void test() throws Exception { 
     Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); 

     while (interfaces.hasMoreElements()) { 
      NetworkInterface networkInterface = interfaces.nextElement(); 
      // drop inactive 
      if (!networkInterface.isUp()) 
       continue; 

      // smth we can explore 
      Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); 
      while(addresses.hasMoreElements()) { 
       InetAddress addr = addresses.nextElement(); 
       System.out.println(String.format("NetInterface: name [%s], ip [%s]", 
         networkInterface.getDisplayName(), addr.getHostAddress())); 
      } 
     } 
    } 
相關問題