簡體   English   中英

使用Java的有線/無線LAN中的IP地址

[英]IP Address in a wired /wireless LAN using java

我正在使用Java服務器-客戶端應用程序,該應用程序在有線或無線LAN內的每個oda之間傳輸文件,現在我的問題是如何在無線或有線LAN中檢測客戶端計算機和服務器計算機的IP地址。 底線:如何使用Java代碼檢測兩台計算機之間有線或無線LAN連接中計算機的IP地址。

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");
 }
}  

也許jgroups可以幫助您: http ://www.jgroups.org

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM