2012-01-11 89 views
0

我正在研究一種在有線或無線局域網內傳輸btw文件的java服務器 - 客戶端應用程序,我現在的問題是如何檢測客戶端計算機的IP地址和無線或有線LAN中的服務器計算機。底線:如何使用java代碼來檢測有線或無線局域網連接btw兩臺計算機中的計算機的ip地址。使用java的有線/無線局域網中的IP地址

+1

你有沒有看到相關的問題嗎? http://stackoverflow.com/questions/2845279/,http://stackoverflow.com/questions/8083479/ – 2012-01-11 12:04:38

回答

0
import java.io.*; 
import java.net.*; 
import java.util.*; 
import static java.lang.System.out; 

public class ListNets { 

public static void main(String args[]) throws SocketException, UnknownHostException { 
    System.out.println(System.getProperty("os.name")); 
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); 
    for (NetworkInterface netint : Collections.list(nets)) 
     if (netint.getName().equals("wlan0") || netint.getName().equals("en0")) { 
      displayInterfaceInformation(netint); 
     }  
} 

static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { 
    out.printf("Display name: %s\n", netint.getDisplayName()); 
    out.printf("Name: %s\n", netint.getName()); 
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); 
    for (InetAddress inetAddress : Collections.list(inetAddresses)) { 

     out.printf("InetAddress: %s\n", inetAddress); 
    } 
    out.printf("\n"); 
} 
} 
相關問題