在C#中,如何解析主机的IP地址?

时间:2022-09-03 21:25:32

How can you dynamically get the IP address of the server (PC which you want to connect to)?

如何动态获取服务器的IP地址(要连接的PC)?

5 个解决方案

#1


System.Dns.GetHostEntry can be used to resolve a name to an IP address.

System.Dns.GetHostEntry可用于将名称解析为IP地址。

#2


IPHostEntry Host = Dns.GetHostEntry(DNSNameString);
DoSomethingWith(Host.AddressList);

#3


If you Use the Below Method you will be able to resolve correctly

如果您使用下面的方法,您将能够正确解决

 public static bool GetResolvedConnecionIPAddress(string serverNameOrURL, out IPAddress resolvedIPAddress)
        {
            bool isResolved = false;
            IPHostEntry hostEntry = null;
            IPAddress resolvIP = null;
            try
            {
                if (!IPAddress.TryParse(serverNameOrURL, out resolvIP))
                {
                    hostEntry = Dns.GetHostEntry(serverNameOrURL);

                    if (hostEntry != null && hostEntry.AddressList != null && hostEntry.AddressList.Length > 0)
                    {
                        if (hostEntry.AddressList.Length == 1)
                        {
                            resolvIP = hostEntry.AddressList[0];
                            isResolved = true;
                        }
                        else
                        {
                            foreach (IPAddress var in hostEntry.AddressList)
                            {
                                if (var.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    resolvIP = var;
                                    isResolved = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    isResolved = true;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                resolvedIPAddress = resolvIP;
            }

            return isResolved;
        }

#4


You want to do an nslookup.

你想做一个nslookup。

Here's an example:

这是一个例子:

http://www.c-sharpcorner.com/UploadFile/DougBell/NSLookUpDB00112052005013753AM/NSLookUpDB001.aspx

#5


Based on your comment on chaos's answer, you don't want the IP address of a server, you want the IP address of a client. If that's the case, fix your question ... and your answer would be HttpRequest.UserHostAddress.

根据您对混沌答案的评论,您不需要服务器的IP地址,您需要客户端的IP地址。如果是这种情况,请修正您的问题......您的答案将是HttpRequest.UserHostAddress。

#1


System.Dns.GetHostEntry can be used to resolve a name to an IP address.

System.Dns.GetHostEntry可用于将名称解析为IP地址。

#2


IPHostEntry Host = Dns.GetHostEntry(DNSNameString);
DoSomethingWith(Host.AddressList);

#3


If you Use the Below Method you will be able to resolve correctly

如果您使用下面的方法,您将能够正确解决

 public static bool GetResolvedConnecionIPAddress(string serverNameOrURL, out IPAddress resolvedIPAddress)
        {
            bool isResolved = false;
            IPHostEntry hostEntry = null;
            IPAddress resolvIP = null;
            try
            {
                if (!IPAddress.TryParse(serverNameOrURL, out resolvIP))
                {
                    hostEntry = Dns.GetHostEntry(serverNameOrURL);

                    if (hostEntry != null && hostEntry.AddressList != null && hostEntry.AddressList.Length > 0)
                    {
                        if (hostEntry.AddressList.Length == 1)
                        {
                            resolvIP = hostEntry.AddressList[0];
                            isResolved = true;
                        }
                        else
                        {
                            foreach (IPAddress var in hostEntry.AddressList)
                            {
                                if (var.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    resolvIP = var;
                                    isResolved = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    isResolved = true;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                resolvedIPAddress = resolvIP;
            }

            return isResolved;
        }

#4


You want to do an nslookup.

你想做一个nslookup。

Here's an example:

这是一个例子:

http://www.c-sharpcorner.com/UploadFile/DougBell/NSLookUpDB00112052005013753AM/NSLookUpDB001.aspx

#5


Based on your comment on chaos's answer, you don't want the IP address of a server, you want the IP address of a client. If that's the case, fix your question ... and your answer would be HttpRequest.UserHostAddress.

根据您对混沌答案的评论,您不需要服务器的IP地址,您需要客户端的IP地址。如果是这种情况,请修正您的问题......您的答案将是HttpRequest.UserHostAddress。