【问题标题】:How can i get the ip address of connection? [duplicate]我怎样才能得到连接的IP地址? [复制]
【发布时间】:2017-12-18 09:03:15
【问题描述】:

我想知道连接的 IP 地址。不是本地 IP。

本地IP:192.168.#.# 广域网 IP:#.#.#.#

您可以在http://whatismyip.org 看到您的 WAN IP

我尝试了这个解决方案,但它提供了本地 IP。

using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace FasterComputer
{
    class Program
    {
        static void Main(string[] args)
        {
            string ip = Ipfind();
            Console.WriteLine(ip);
            Console.ReadLine();
        }
        static string Ipfind()
        {
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                return "Disconnected";
            }

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

            return Convert.ToString(host
                .AddressList
                .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork));
        }
    }
}

【问题讨论】:

标签: c# networking tcp ip


【解决方案1】:

如果您想要您的公共 IP,您必须离开您的网络。为什么你的代码会隐式知道你的 public IP?

谢天谢地,您可以使用icanhazip 等服务

string publicIp = new WebClient().DownloadString("http://icanhazip.com");            

【讨论】:

  • 谢谢。很有帮助!
猜你喜欢
  • 1970-01-01
  • 2012-10-02
  • 2013-02-14
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
  • 1970-01-01
相关资源
最近更新 更多