2016-03-01 47 views
0

我正在開發一個充當服務器的Web應用程序。在這個應用程序中,我使用ServerSocket實現了服務器應用程序和充當客戶端的wi-fi設備之間的雙向通信。我沒有直接連接到我的服務器,我正在連接到我的路由器,哪個端口轉發到我的系統,雙向通信成功。我現在想要的是找到連接的Wi-fi設備的MAC地址。我已經做了一些研究,並試圖獲得MAC地址,但失敗了。任何人都可以幫助我做到這一點。以下是我的代碼的一部分。如何找到連接到插座的Wi-Fi設備的MAC地址

public class ScheduleJob extends ServletApp implements Job{ 

private int port = 1717; 
public static String number; 
String ReceivedData = ""; 

public void execute(JobExecutionContext context)throws JobExecutionException { 
    System.out.println("Starting ... "); 

    ServerSocket Sersocket = null; 
    System.out.println("Starting the socket server at port:" +port); 
    boolean listeningSocket = true; 
    try { 
     Sersocket = new ServerSocket(port); 
     System.out.println("Waiting for clients..."); 
    } catch (IOException e) { 
     System.err.println("Could not listen on port: 1717"); 
    } 
    try { 

     while (listeningSocket) { 
      Socket scokt = Sersocket.accept(); 
      InetAddress MachineAdd = scokt.getInetAddress(); 
      System.out.println("Response-----:" +MachineAdd); 
      InetAddress ip = InetAddress.getLocalHost(); 
      System.out.println("current ip : "+ip); 
      NetworkInterface network = NetworkInterface.getByInetAddress(ip); 

      byte[] mac = network.getHardwareAddress(); 
      System.out.print("Current MAC address : "); 

      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < mac.length; i++) { 
       sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));   
      } 
      System.out.println(sb.toString()); 
+0

我我正在獲取我的PC的MAC地址而不是Wi-Fi設備。 – Arun

回答

2

MAC地址是屬於layer 2 of OSI model的一條信息。

換句話說,無論何時使用L3協議進行通信(直到通過交叉電纜或通過L2交換機直接進行通信),它都不會被保留。

我建議您將MAC地址作爲您的應用程序通信的一部分從客戶端發送到您的服務器,所以當您的路由器執行端口轉發時它不會丟失。

一些已經問關於這個主題的問題:

+0

我有一個小小的懷疑。還有其他的東西,比如每個設備獨有的MAC ID,並且可以在沒有客戶端發送的情況下從服務器端讀取。 – Arun

+0

在L3通信中,您只能獲得客戶端IP和TCP端口。 – rkosegi

+0

謝謝你的朋友。如您所說,我會嘗試將MAC ID作爲客戶通信的一部分。 – Arun

相關問題