문제

내 asp.net 페이지를 호출하는 서버의 IP 주소를 어떻게 얻습니까? 응답 객체에 대한 내용을 보았지만 C#에서 매우 새롭습니다. 엄청 고마워.

도움이 되었습니까?

해결책

이것은 작동해야합니다 :

 //this gets the ip address of the server pc

  public string GetIPAddress()
  {
     IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.
     IPAddress ipAddress = ipHostInfo.AddressList[0];

     return ipAddress.ToString();
  }

http://wec-library.blogspot.com/2008/03/gets-ip-address-of-server-pc-using-c.html

또는

 //while this gets the ip address of the visitor making the call
  HttpContext.Current.Request.UserHostAddress;

http://www.geekpedia.com/kb32_how-do-i-get-the-visitors-ip-address.html

다른 팁

Request.ServerVariables["LOCAL_ADDR"];

이것은 IP에게 다중 호메드 서버에 대한 요청이 시작됩니다.

DNS 호출이 필요하므로 위의 속도는 느립니다 (가능하지 않으면 작동하지 않습니다). 아래 코드를 사용하여 해당 서브넷 마스크와 함께 현재 PC의 로컬 IPv4 주소의 맵을 얻을 수 있습니다.

public static Dictionary<IPAddress, IPAddress> GetAllNetworkInterfaceIpv4Addresses()
{
    var map = new Dictionary<IPAddress, IPAddress>();

    foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (var uipi in ni.GetIPProperties().UnicastAddresses)
        {
            if (uipi.Address.AddressFamily != AddressFamily.InterNetwork) continue;

            if (uipi.IPv4Mask == null) continue; //ignore 127.0.0.1
            map[uipi.Address] = uipi.IPv4Mask;
        }
    }
    return map;
}

경고 : 이것은 아직 모노에서 구현되지 않았습니다

  //this gets the ip address of the server pc
  public string GetIPAddress()
  {
     string strHostName = System.Net.Dns.GetHostName();
     //IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); <-- Obsolete
     IPHostEntry ipHostInfo = Dns.GetHostEntry(strHostName);
     IPAddress ipAddress = ipHostInfo.AddressList[0];

     return ipAddress.ToString();
  }

이것은 IPv4에서 작동합니다 :

public static string GetServerIP()
{            
    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

    foreach (IPAddress address in ipHostInfo.AddressList)
    {
        if (address.AddressFamily == AddressFamily.InterNetwork)
            return address.ToString();
    }

    return string.Empty;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top