通过提供远程计算机主机名获取远程计算机IP地址 [英] Getting remote machine IP address by giving remote machine Hostname

查看:303
本文介绍了通过提供远程计算机主机名获取远程计算机IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


以下代码运行良好,它返回本地计算机的本地主机名和IP地址。


  string strHostName = Dns.GetHostName();


IPAddress [] addr1 = Dns.GetHostAddresses(strHostName);

       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; for(int i = 0; i< addr1.Length; i ++)

            {

                Console.WriteLine("检查InterNetwrok");

                if(addr1 [i] .AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)

            &NBSP;&NBSP;&NBSP;&NBSP; Console.WriteLine("IP地址{0}:{1},使用GetHostAddresses",i,addr1 [i] .ToString());

      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


我的问题是,如果我配置一个远程机器主机名,那么我应该得到该机器的IP地址。我硬编码strHostname(远程机器的一个)然后异常来了" ;没有主人知道'。


任何人都可以通过提供机器的主机名来帮助我获取远程机器的IP地址。



解决方案

找到主机后,您的代码永远不会退出for循环。  ;试试这个


string strHostName = Dns.GetHostName();
$
IPHostEntry LocalHostIPEntry = Dns.GetHostEntry(strHostName);

IPAddress LocalHostIP = LocalHostIPEntry.AddressList [0];
$
Console.WriteLine(" IP Address {0}:{1},Using GetHostAddresses",LocalHostIP.ToString());


Following code runs well and it returns the local host name and IP address of Local machine.

 string strHostName = Dns.GetHostName();

IPAddress[] addr1 = Dns.GetHostAddresses(strHostName);
            for (int i = 0; i < addr1.Length; i++)
            {
                Console.WriteLine("checking InterNetwrok ");
                if(addr1[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork )
                Console.WriteLine("IP Address {0}: {1}, Using GetHostAddresses ", i, addr1[i].ToString());
            }

My problem is, if I configure a remote machine host name then I should get the IP address of that machine.I hard coded the strHostname ( remote machine's one) then exception came as " NO HOST IS KNOWN'".

Can any one please help me how to get remote machine IP address by giving Hostname of the machine.

解决方案

Your code never never exits the for loop when the host is found.  Try this instead

string strHostName = Dns.GetHostName();
IPHostEntry LocalHostIPEntry = Dns.GetHostEntry(strHostName);
IPAddress LocalHostIP = LocalHostIPEntry.AddressList[0];
Console.WriteLine("IP Address {0}: {1}, Using GetHostAddresses ", LocalHostIP.ToString());


这篇关于通过提供远程计算机主机名获取远程计算机IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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