服务端得到客户端的IP和主机

   如何从服务端获取客户端的IP和主机

   首先,建立一条WebService,其中包含了获取客户端IP的方法,通过Request的ServerVariables方法获取web服务器变量的集合,HTTP_VIA   和 HTTP_X_FORWARDED_FOR 这两个 ServerVariables表示使用了代理,

 

 /// <summary>
        /// 获得ClientIP
        /// </summary>
        /// <param name="strClientName">客户端机器名</param>
        /// <returns></returns>
        [WebMethod] 
        public  string  GetClientIP(string  strClientName) 
        {
            // 或者客户端名 和客户端IP
            string strYourName = strClientName;
            string strIP = string.Empty;

            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                strIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                strIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

            }

            return strIP;
}
 
 2.当获取了IP后,需要对IP进行保存,通常我们会将客户端名和客户端IP写入客户端ArrayList中,然后将对应的名称与IP保存起来
 /// <summary>
        /// 保存ClientIP
        /// </summary>
        /// <param name="strClientName"></param>
        /// <param name="strIP"></param>
        /// <param name="strConnectionType"></param>
        /// <returns></returns>
        [WebMethod]
        public void SaveClientIP(string strClientName, string strIP, string strConnectionType)
        {
            // 或者客户端名 和客户端IP
            string strYourName = strClientName;
            

            // 客户端ArrayList中有值的场合
            if (alClient != null && alClient.Count != 0)
            {
                // 将客户端名和客户端IP存入 客户端ArrayList中
                for (int i = 0; i < alClient.Count; i++)
                {
                    string[] strClient = new string[3];
                    strClient = (string[])alClient[i];
                    // 判断机器名存在并且IP不同的场合把当前的IP 存入 相应的机器名
                    if (strClient[0] == strClientName && strClient[1] != strIP)
                    {
                        strClient[1] = strIP;
                        strClient[2] = strConnectionType;
                        alClient.RemoveAt(i);
                        alClient.Add(strClient);
                        return;
                    }
                    else if (strClient[0] == strClientName && strClient[1] == strIP)
                    {
                        return;
                    }
                }
            }

            // 如果得到的机器名 在客户端ArrayList中不存在的话添加到客户端ArrayList中
            string[] strClientTemp = new string[3];
            strClientTemp[0] = strClientName;               // 机器名
            strClientTemp[1] = strIP;                       // 机器IP
            strClientTemp[2] = strConnectionType;           // 
            alClient.Add(strClientTemp);
            return ;
        }
  3.服务端调用,通过循环将保存的数据读写出来
    
 for (int i = 0; i < Service1.alClient.Count; i++)
                    {
                        //string[] strClinet = new string[2];
                        //strClinet = (string[])Service1.alClient[i];
                        //strClientShow += "客户端机器名:" + strClinet[0] + "    |    客户端ip:" + strClinet[1] + "<br />";
                        string strIP = string.Empty;
                        string strWebIPURL = string.Empty;
                        string strHTML = string.Empty;
                        string strKey = string.Empty;
                        string[] strClinet = new string[3];
                        strClinet = (string[])Service1.alClient[i];
                        strClientShow += "客户端机器名:" + strClinet[0] + "    |    客户端ip:" + strClinet[1] + "    |    " + strClinet[2] + "<br />";

                    }
posted @ 2010-11-02 18:19  marr  阅读(409)  评论(0编辑  收藏  举报