查找本地主机的IP地址 [英] Finding IP address of localhost

查看:313
本文介绍了查找本地主机的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

Hi friends,

How do I find the ipaddress of a localhost like 122.164.97.87.?

推荐答案

Hi.
您想知道自己的IP还是组建另一台计算机?
对于这种情况,我使用以下代码:

Hi.
Do you want to know your own IP or form an other computer?
For this case i use this code:

Imports system.net
Public Shared Function GetIP(Optional ByVal computer As String = "") As ArrayList
    Dim computername As String
    Dim host As IPHostEntry
    Dim hostadresses() As IPAddress
    Dim i As Integer = 0
    Dim IPs As ArrayList = New ArrayList

    If computer = String.Empty Then
        computername = Dns.GetHostName()
    Else
        computername = computer
    End If

    host = Dns.GetHostEntry(computername)
    hostadresses = host.AddressList()
    For i = 0 To hostadresses.GetLength(0) - 1
        IPs.Add(hostadresses(i).ToString())
    Next

    Return IPs
End Function



如果将可选Byval"计算机保留为空,则将获得自己的IP地址.
如果另一台计算机的DNS名称是已知的->您可以获得IP.
该函数在ArrayList中返回Ip地址.

我希望我可以为这个代码示例提供帮助:cool:


问候,
Z



If you leave the Optional Byval computer empty, you will get your own IP-Adress.
If the DNS-Name of an other computer is known -> you can get the IP.
The Function returns the Ip-Adresses in an ArrayList.

I hope I can help with this code-sample :cool:


Regards,
Z


请参阅以下Codeproject的文章(我知道它是C#,但是对VB.NET的翻译应该很简单): [ http://www.codeproject.com/Messages/3479471/Re-My- vote-of-1.aspx [ ^ ].

:)
See the following Codeproject''s article (I know it is C#, but translation to VB.NET should be straightforward): "How To Get IP Address Of A Machine"[^].

P.S.: Make sure to check out the following remark in the article''s follow up:
http://www.codeproject.com/Messages/3479471/Re-My-vote-of-1.aspx[^].

:)


添加名称空间System.net

放置以下代码后

add name space System.net

after place bellow code

public void UseDNS()
{
   string hostName = Dns.GetHostName();
   Console.WriteLine("Host Name = " + hostName);
   IPHostEntry local = Dns.GetHostByName(hostName);
   foreach(IPAddress ipaddress in local.AddressList)
   {
      Console.WriteLine("IPAddress = " + ipaddress.ToString());
   }
}


这篇关于查找本地主机的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆