ServerSocket如何绑定外网的IP,我已经有一个云主机(拥有公网的ip)
我在做一个练习的时候,遇到一个问题。
try{
serverSocket = new ServerSocket(tcpPort);
}catch(IOException e){
e.printStackTrace();
}
try{
ipAddress= InetAddress.getLocalHost();
}catch(UnknownHostException e){
}
ip=ipAddress.getHostAddress();
/**
* 获取请求IP地址
* @return
*/
protected final String getIp(){
HttpServletRequest request=getRequest();
String ip=request.getHeader("x-forwarded-for");
if(ip==null
[解决办法]
ip.length()==0
[解决办法]
"unknown".equalsIgnoreCase(ip)){
ip=request.getHeader("Proxy-Client-IP");
}
if(ip==null
[解决办法]
ip.length()==0
[解决办法]
"unknown".equalsIgnoreCase(ip)){
ip=request.getHeader("WL-Proxy-Client-IP");
}
if(ip==null
[解决办法]
ip.length()==0
[解决办法]
"unknown".equalsIgnoreCase(ip)){
ip=request.getRemoteAddr();
}
return ip;
}