C#获取ID、端口资料搜集

1.C# 获取客户端IP

public static string GetRealIP()
    {
        string ip;
        try
        {
            HttpRequest request = HttpContext.Current.Request;

            if (request.ServerVariables["HTTP_VIA"] != null)
            {
                ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
            }
            else
            {
                ip = request.UserHostAddress;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

        return ip;
    }

    public static string GetViaIP()
    {
        string viaIp = null;

        try
        {
            HttpRequest request = HttpContext.Current.Request;

            if (request.ServerVariables["HTTP_VIA"] != null)
            {
                viaIp = request.UserHostAddress;
            }

        }
        catch (Exception e)
        {

            throw e;
        }

        return viaIp;
    }

原文地址:http://www.cnblogs.com/cole2295/archive/2009/07/11/1520928.html

2.c#获取客户端的IP和服务器的IP

利用c#获取客户端的IP和服务器的IP是很简单的,代码如下:

//获取客户端的IP地址
   TextBox1.Text = Request.UserHostAddress;
   //获取服务器的主机名
   string hostName = Dns.GetHostName();
   //获取服务器的IP地址
   IPAddress[] ips;
   ips = Dns.GetHostAddresses(hostName);
   foreach ( IPAddress ip in ips)
   {
         TextBox2.Text = ip.ToString();
   }

最简单的方法:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress ServerIp = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
            string   strHostIP="";  
            IPHostEntry   oIPHost=Dns.Resolve(Environment.MachineName);
            for (int i = 0; i < oIPHost.AddressList.Length; i++)
            {
                strHostIP = oIPHost.AddressList[i].ToString();
                Console.WriteLine(strHostIP);
            }
            Console.Read();
        }
    }
}

主义Dns.Resolve()方法在.net2.0中已经过时,应该用GetHostEntry取得。

MSDN中的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IPHostEntry host;

            host = Dns.GetHostEntry(Dns.GetHostName()); //Dns.GetHostName()获取本机的机器名,GetHostEntry返回IPHostEntry 实例。

            Console.WriteLine("GetHostEntry({0}) returns:", Dns.GetHostName());

            foreach (IPAddress ip in host.AddressList)
            {
                Console.WriteLine(ip); //向控制台打印得到的所有的IP地址。
            }

      }
    }
}

IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());

            string myip = IpEntry.AddressList[0].ToString();

这样,如果没有安装IPV6协议,可以取得IP地址. 但是如果安装了IPV6,就取得的是IPV6的IP地址.

string myip = IpEntry.AddressList[1].ToString();

这样就在IPV6的情况下取得IPV4的IP地址.

把IP地址变成String类型,然后通过长度判断,IPv4的长度最多只有15位,IPv6有39位。
 
 #region //IPV4正则表达式函数
        public static bool IsIPv4Address(string ip)
        {

string pattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
            Regex r = new Regex(pattern);
            Match m = r.Match(ip);

              return m.Success;
        }
 #endregion
原文地址:http://blog.163.com/lxg_5027/blog/static/39212532009511111741768/
3.获取客户端IP、端口(Sockets):
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;//为了IPEndPoint而添加的引用
using System.Net.Sockets;

namespace GetClntIP
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener tcpListener = new TcpListener(9000);//监听的端口号,可根据需要修改
            tcpListener.Start();

            //loop for listen
            while (true)
            {
                Socket sock = tcpListener.AcceptSocket();

                //服务器当前时间
                DateTime connTime = DateTime.Now;
                Console.WriteLine("The time is :"+connTime.ToString());

                System.Net.IPAddress ipAdd;
                int port;

                //获得连接当前用户的IP以及端口号
                ipAdd = (sock.RemoteEndPoint as IPEndPoint).Address;
                port = (sock.RemoteEndPoint as IPEndPoint).Port;

                Console.WriteLine("The client IP :"+ipAdd.ToString ());
                Console.WriteLine("The client port :" + port.ToString());
            }

            tcpListener.Stop();
        }
    }
}
另:
 
   

[附]获取本地IP:

//获得本地局域网IP地址

IPAddress [] AddressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;

if(AddressList.Length<1)

{

       retern "";

}

retern AddressList[0].ToString();//注意这里数组参数为0

//获得拨号动态分配IP地址

IPAdress [] AddressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;

if(AddressList.Length<2)

{

        return "";

}

return AddressList[1].ToString();//注意这里数组参数为1

原文地址:http://hi.baidu.com/luanhui2007/blog/item/2a9e593903dd1cfab311c70f.html

4.ASP.NET获取IP的方法
 
  

在ASP中使用 Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正的客户端IP地址。

要想透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取。

不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的IP。

还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables ("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......

//方法一
HttpContext.Current.Request.UserHostAddress;

//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

//方法四(无视代理)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

客户端:

//方法五
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert("Your IP address is "+ip);

//方法六(无视代理)
function GetLocalIPAddress()
{
    var obj = null;
    var rslt = "";
    try
    {
        obj = new ActiveXObject("rcbdyctl.Setting");
        rslt = obj.GetIPAddress;
        obj = null;
    }
    catch(e)
    {
        //
    }
    
    return rslt;
}

来自印度的MCT Maulik Patel提供了一种服务端的解决方案,很好:

if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // 服务器, using proxy
{
       得到真实的客户端地址
     ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can't get the Client IP
{
     
      得到服务端的地址
     ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
有些客户端会因为“header_access deny”的安全设置而不发给我们Ip

原文地址:http://hi.baidu.com/dummy2123456/blog/item/3adbfdc7d82c8cd4d100604a.html

 

转载于:https://www.cnblogs.com/heaven.kaixin/archive/2010/09/17/2052413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值