我在做一个练习的时候,遇到一个问题。
try{
serverSocket = new ServerSocket(tcpPort);
}catch(IOException e){
e.printStackTrace();
}
try{
ipAddress= InetAddress.getLocalHost();
}catch(UnknownHostException e){
}
ip=ipAddress.getHostAddress();
如果这段代码在我自己的本地使用,获取的是局域网ip这个我可以理解。但是我把这段代码放到拥有独立ip的云主机上,为什么拿到的还是私网ip呢,不是应该获取得到的公网ip吗?
有没有现成的代码提供参考解析,谢谢了
java socketserver socket
------解决方案--------------------
ipAddress= InetAddress.getLocalHost();
------解决方案--------------------
楼主的服务器是什么系统的?
------解决方案--------------------
/**
* 获取请求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;
}
------解决方案--------------------
Socket socket = new Socket(ip字符串,端口);
------解决方案--------------------
做梦呢~~ 按你的想法 云主机就把公网ip用完了,别人不要用了
------解决方案--------------------
看过了,学习下!