我正在开发一个java服务器-客户端应用程序,它在有线或无线LAN中传输文件,但我现在的问题是如何检测无线或有线LAN中的客户端计算机和服务器计算机的IP地址。底线:如何使用java代码在两台计算机的有线或无线LAN连接中检测计算机的ip地址。
发布于 2012-01-11 14:20:17
也许jgroup可以帮助你:http://www.jgroups.org
发布于 2016-03-03 09:20:52
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");
}
}
https://stackoverflow.com/questions/8818832
复制