如何从浏览器获取连接的网站IP地址?

时间:2015-05-30 13:51:01

标签: c# network-programming ip-address detection

我试图检测哪个网站用户连接到..

我试图获得tcp连接,我解析了它们,例如我试图检测facebook。但是当我退出并关闭Facebook时,它仍然显示31.13.93.3(facebook的IP)

这是我的代码..

public partial class Form1 : Form
{
    static string faceIP = "31.13.93.3";
    static string _targetIP,_targetPORT,_connectedWebSiteIP,_connectedWebSitePORT = string.Empty;
    static string[] splitted = null;
    public Form1()
    {
        /* 127.0.0.1:5037:127.0.0.1:49569

         * First = 127.0.0.1
         * Second = 5037
         * Third = 127.0.01
         * Fourth = 49569
         */
        InitializeComponent();
        this.Name = "Active Tcp Connections";
        if (findFacebookIP())
        {
            MessageBox.Show("You opened or connected to facebook page!");
        }
    }
    public static bool findFacebookIP(){
        IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
        TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
        foreach (TcpConnectionInformation c in connections)
        {
            string tcpCon = string.Format("{0}:{1}", c.LocalEndPoint.ToString(), c.RemoteEndPoint.ToString());
            splitted = tcpCon.Split(':');
            _targetIP = splitted[0]; // Main Machine ip adress / local ip address (First)
            _targetPORT = splitted[1]; // Main machine port number (Second)
            _connectedWebSiteIP = splitted[2]; // (Third)
            _connectedWebSitePORT = splitted[3]; // (Fourth)
            if (_connectedWebSiteIP == faceIP)
            {
                return true;
            }
        }
        return false;
    }
    // face ip = 31.13.93.3
}

另外我需要一直运行它背景因为这个方法只是打开工作..你可以看到我在Form1()构造函数方法中写了它。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

使用TcpState检查:

foreach (TcpConnectionInformation c in connections)
{
    //------------rest of your code
    if(c.State == TcpState.Closed)
         return false;
    //------------rest of your code
}

要在后台运行BackgroundWorker