`
achieve2015
  • 浏览: 2423 次
社区版块
存档分类
最新评论

get client IP address

阅读更多
public String getIpAddr(HttpServletRequest request){
String ipAddress = null;   
       
      ipAddress = request.getHeader("x-forwarded-for");   
     if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {   
      ipAddress = request.getHeader("Proxy-Client-IP");   
     }  
      if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {   
         ipAddress = request.getHeader("WL-Proxy-Client-IP");   
    }  
      if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {   
      ipAddress = request.getRemoteAddr();   
      if(ipAddress.equals("127.0.0.1")){   
     
       InetAddress inet=null;   
     try {   
     inet = InetAddress.getLocalHost();  
     } catch (UnknownHostException e) {   
     e.printStackTrace();  
   }  
    ipAddress= inet.getHostAddress();  
      }  
           
     }  

      //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割   
      if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15   
          if(ipAddress.indexOf(",")>0){   
             ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));   
        }  
     }  
    return ipAddress;    

}
分享到:
评论

相关推荐

    TCP/UDP网络调试助手

    foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); ip = _IP...

    api-client:DB-IP地址查找服务的客户端库

    dbip-api-client ... 有关API响应的更多详细...$ ipAddress = $ argv [ 1 ] or die ( "usage: {$argv[0]} <ip>\n" ); // lookup IP address information $ addrInfo = DBIP \ Address :: lookup ( $ ipAddress ); var_

    python-socket-udp-server-client.zip_For You I Will_Network_can p

    2 I run the two py file code in two machines,first, you should get to know the ip address of server machine, and replace the HOST constant with your own server ip, then type message firstly on ...

    php限制ip地址范围的方法

    function get_real_ipaddress() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { return $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { return $_SERVER['...

    Socket简单通讯

    IPAddress ip = IPAddress.Any; ServerInfo = new IPEndPoint(ip, this.GetPort()); ServerSocket.Bind(ServerInfo);//将SOCKET接口和IP端口绑定 ServerSocket.Listen(10);//开始监听,并且挂起数为10 ...

    win 3.11 for workgroup tcpip支持

    "The DHCP client was unable to obtain an IP network address from a DHCP server. Do you want to see future DHCP messages?" This message means that TCP/IP has initialized but without any addressing ...

    仿飞秋,局域网聊天工具

    IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port); byte[] sen = Class2.senDataToByte(sendDatas); udpclient.Send(sen, sen.Length, iep); } /// /// 获取本机IP /// public void IPselect...

    C# 邮件服务器

    [your vitural server]>system>services, enable smtp and pop3 services and set ipaddress binding with or without ssl/tls. the host name is required when set bindings. eg. bind smtp service to smtp.[your...

    the simple FTP file by desgining c

    This programma achieves a simple FTP process. the client is able to get file or updata file from server. the ip address should be localhost

    w5100驱动程序

    NetConf.gwip = *((u_long*)GET_GW_IP); NetConf.sn = *((u_long*)GET_SN_MASK); NetConf.dns = *((u_long*)GET_DNS_IP); PRINTLN("Get network information from DHCP Server..."); ...

    MakeTcpHole

    有一个私有网络10.*.*.*,Client A是其中的一台计算机,这个网络的网关(一个NAT设备)的外网IP是155.99.25.11(应该还有一个内网的IP地址,比如10.0.0.10)。如果Client A中的某个进程(这个进程创建了一个UDP ...

    lwip源码(小型TCP/IP协议栈)

    * DHCP, AutoIP/APIPA (Zeroconf), ACD (Address Conflict Detection) and (stateless) DHCPv6 * UDP (User Datagram Protocol) including experimental UDP-lite extensions * TCP (Transmission Control ...

    DotNetFtpSources

    FTP_Listener = new TcpListener(System.Net.IPAddress.Parse(this.IPAddress),this.Port); } //D閙arrage de l'閏oute FTP_Listener.Start(); this.m_listening=true; while(true) { /...

    计算机网络第六版答案

    The packet switch uses the destination IP address in the packet to determine the outgoing link. Asking which road to take is analogous to a packet asking which outgoing link it should be forwarded on...

    udp 和tcp结合视频监控系统.zip

    udpClient.BeginSend(datagram, datagram.Length, Address, Port, SendCompleted, udpClient); } } } } private void SendCompleted(IAsyncResult ar) { UdpClient udp = ar.AsyncState as Udp...

    delphi socket call php socket 例子

    echo "New client connected: { $ip } \n" ; // remove the listening socket from the clients-with-data array $key = array_search ( $sock , $read ); unset( $read [ $key ]); } // loop through all...

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Part 1: Introduction and TCP/IP Chapter 1. Introduction Section 1.1. Introduction Section 1.2. A Simple Daytime Client Section 1.3. Protocol Independence Section 1.4. Error Handling: ...

    ASP.NET简单获取服务端和客户端计算机名称的方法

    System.Net.IPAddress clientIP = System.Net.IPAddress.Parse(Request.UserHostAddress);//根据目标IP地址获取IP对象 System.Net.IPHostEntry ihe = System.Net.Dns.GetHostEntry(clientIP);//

    KISDJ语音聊天

    EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); byteData = new byte[1024]; //Receive data asynchornously. clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, Socket...

Global site tag (gtag.js) - Google Analytics