【问题标题】:How to get connected web site ip adress from browser?如何从浏览器获取连接的网站 IP 地址?
【发布时间】:2015-08-13 08:21:48
【问题描述】:

我正在尝试检测连接到哪个网站的用户..

我尝试获取 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() 构造函数方法中编写了它。 感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    使用TcpState检查:

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

    对于后台运行,请使用BackgroundWorker

    【讨论】:

    • 不客气。但如果使用我的解决方案解决了,请标记我的答案。 :)
    • 抱歉,这不是你的答案 :) 谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-08-04
    • 2016-06-25
    • 2010-10-19
    • 2017-06-20
    • 2020-04-12
    • 2015-09-07
    相关资源
    最近更新 更多