java中怎么获取客户端的真实的ip和端口号

如题所述

第1个回答  2016-08-08
public static String getHostIpAddress() {
String hostIp = "";
InetAddress netAddress = getInetAddress();
hostIp = getHostIp(netAddress);
return hostIp;
}
public static InetAddress getInetAddress() {
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
System.out.println("unknown host!");
}
return null;
}
public static String getHostIp(InetAddress netAddress) {
if (null == netAddress) {
return null;
}
String ip = netAddress.getHostAddress(); // get the ip address
return ip;
}
public static String getHostName(InetAddress netAddress) {
if (null == netAddress) {
return null;
}
String name = netAddress.getHostName(); // get the host address
return name;
}本回答被提问者采纳
相似回答