如何确定我的计算机的 IP 地址是否属于指定的网络地址?

发布于 2024-10-21 02:39:39 字数 577 浏览 3 评论 0原文

        string myHost = System.Net.Dns.GetHostName();

        string myIP = 
          System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
        MessageBox.Show(myIP);

        TextReader read = new StreamReader(//Text file with network address);
        var ip = read.ReadLine();
        read.Close();

        if (ip == //Need help with this part)
            MessageBox.Show("You are on the network");
        else
            MessageBox.Show("You are not on the network");

我可以获得我的计算机地址,但我需要将其与网络地址进行比较,如果它属于该网络地址,则表明它属于该网络地址。

        string myHost = System.Net.Dns.GetHostName();

        string myIP = 
          System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
        MessageBox.Show(myIP);

        TextReader read = new StreamReader(//Text file with network address);
        var ip = read.ReadLine();
        read.Close();

        if (ip == //Need help with this part)
            MessageBox.Show("You are on the network");
        else
            MessageBox.Show("You are not on the network");

I can get my computers address but I need to compare it to a network address and if it falls under that network address to show that it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨笨の傻瓜 2024-10-28 02:39:39

如果您的程序在 IP 无效时崩溃,您可以将其更改为检查该异常的 try catch(运行它并使其崩溃以查找异常)。这就是我在与 Lidgren 的多人游戏中验证 IP 的方法。

尽管这可能不是最好的方法,但它确实有效。

If your program crashes when your IP is invalid, you can change it over to a try catch that checks for that exception (run it and crash it to find the exception). This is how I validated the IP in my multiplayer with Lidgren.

Though this is probably not the best way, it works.

忆伤 2024-10-28 02:39:39

查看 ip 是否在某个范围内的一种简单方法是将 ip 以及范围的开头和结尾转换为 long(可以编写一个函数以将 IP 地址转换为 long)。然后查看ip是否在两个数字之间。

例如:

要检查的IP:172.17.1.25 -- 转换为long --> 172017001025

范围:

Start - 172.0.0.0 -- 转换为 long --> 172000000000

End - 172.255.255.255 -- 转换为 long --> 172255255255

现在只需检查 ip 是否在开始值和结束值之间。

One simple way to see if an ip is in a range is to convert the ip and the start and end of the range to longs (a function can be written for the conversion to a long from an IP address). Then see if the ip is between the two numbers.

For example:

IP to check: 172.17.1.25 -- Converted to long --> 172017001025

Range:

Start - 172.0.0.0 -- Converted to long --> 172000000000

End - 172.255.255.255 -- Converted to long --> 172255255255

Now just check if the ip is between the start and end values.

吻泪 2024-10-28 02:39:39

我推荐可以在 codeplex 上找到的 IPNetwork 实用程序。它非常灵活且功能强大。它将允许您做很多事情,确定给定 IP 地址的网络就是其中之一。

http://ipnetwork.codeplex.com/

代码应如下所示:

    var ipnetwork = IPNetwork.Parse(ip);
    if (ipnetwork.Contains(myIp)) {
        // do some work...

I recommend the IPNetwork utility that can be found on codeplex. It is pretty flexible and powerful. It will allow you to do a lot of things, and determining the network for a given ip address is one of them.

http://ipnetwork.codeplex.com/

The code should look like :

    var ipnetwork = IPNetwork.Parse(ip);
    if (ipnetwork.Contains(myIp)) {
        // do some work...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文