0

I wanna know IP address of connection. Not Local IP.

Local IP: 192.168.#.# WAN IP: #.#.#.#

You see your WAN IP at http://whatismyip.org

I tried this soution but that gives Local 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));
        }
    }
}
AliFurkan
  • 487
  • 5
  • 11

1 Answers1

2

If you want your public IP you have to go outside your network. Why would your code implicitly know your public IP?

Thankfully you can use services like icanhazip

string publicIp = new WebClient().DownloadString("http://icanhazip.com");            
maccettura
  • 10,514
  • 3
  • 28
  • 35