2013-07-31 135 views
-2

获取C#2010中的服务器名称和IP地址获取C#2010中的服务器名称和IP地址

我想获取服务器的IP地址。以下代码来自:

public static void DoGetHostEntry(string hostname) 
{ 

     IPHostEntry host; 

     host = Dns.GetHostEntry(hostname); 

     MessageBox.Show("GetHostEntry({0}) returns:"+ hostname); 

     foreach (IPAddress ip in host.AddressList) 
     { 
      MessageBox.Show(" {0}"+ ip.ToString()); 
     } 
} 

此代码必须知道服务器计算机的名称。
AddressFamily在System.Net.IPAddress

System.Net.IPAddress i;
string HostName = i.AddressFamily.ToString();

错误------------->未分配的局部变量的 '我'

如何使用我可以得到服务器计算机的名称吗?

+0

您需要更新您的问题以更准确地描述您所问的内容(将问题的答案移至问题的评论)。 – Jake1164

+0

我在你的本地网络上有软件。网络中只有一台服务器。 该软件可能安装在许多局域网上。并利用它。 我想安装并在任何位置运行;自动检测本地网络上的服务器计算机;;;;我没有服务器的IP和计算机名称。我希望他们得到一个特殊的代码。;;;;服务器是计算机名称和IP地址。在客户端计算机上,服务器将识别。并返回IP地址或名称。 –

回答

-1
public string[] ServerName() 
    { 
     string[] strIP = DisplayIPAddresses(); 
     int CountIP = 0; 
     for (int i = 0; i < strIP.Length; i++) 
     { 
      if (strIP[i] != null) 
       CountIP++; 
     } 
     string[] name = new string[CountIP]; 
     for (int i = 0; i < strIP.Length; i++) 
     { 
      if (strIP[i] != null) 
      { 
       try 
       { 
        name[i] = System.Net.Dns.GetHostEntry(strIP[i]).HostName; 
       } 
       catch 
       { 
        continue; 
       } 
      } 
     } 
     return name; 
    } 


    public string[] DisplayIPAddresses() 
    { 
     StringBuilder sb = new StringBuilder(); 
     // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)  
     NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); 

     int i = -1; 
     string[] s = new string[networkInterfaces.Length]; 

     foreach (NetworkInterface network in networkInterfaces) 
     { 
      i++; 
      if (network.OperationalStatus == OperationalStatus.Up) 
      { 
       if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue; 
       if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue; 
       //GatewayIPAddressInformationCollection GATE = network.GetIPProperties().GatewayAddresses; 
       // Read the IP configuration for each network 

       IPInterfaceProperties properties = network.GetIPProperties(); 
       //discard those who do not have a real gateaway 
       if (properties.GatewayAddresses.Count > 0) 
       { 
        bool good = false; 
        foreach (GatewayIPAddressInformation gInfo in properties.GatewayAddresses) 
        { 
         //not a true gateaway (VmWare Lan) 
         if (!gInfo.Address.ToString().Equals("0.0.0.0")) 
         { 
          s[i] = gInfo.Address.ToString(); 
          good = true; 
          break; 
         } 
        } 
        if (!good) 
        { 
         continue; 
        } 
       } 
       else 
       { 
        continue; 
       } 
      } 
     } 
     return s; 
    } 
0

要获取主机的名称,你可以做到以下几点:

string name = System.Net.Dns.GetHostName(); 

如果你想要的主机名和计算机(第一个IPv4)IP使用以下命令:

string name = System.Net.Dns.GetHostName(); 
host = System.Net.Dns.GetHostEntry(name); 
System.Net.IPAddress ip = host.AddressList.Where(n => n.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First(); 

的名称和该IP将保存本地计算机的信息。

服务器然后可以通过UDP多播和网络将刚刚加入的是不特定于服务器的一个已知的多播地址的客户端发出的IP。

多播example

+0

如何获取IP? 该名称来自计算机的IP地址。 我没有IP而没有计算机名称服务器。 –

+0

你想要一台没有IP或名字的计算机的名字吗? – Jake1164

+0

不是。服务器是计算机名称和IP地址。 在客户端计算机上,服务器将识别。并返回IP地址或名称。 –

0

首先,你需要找出自己的错误(未分配的本地变量),并了解它为什么会到来(这是非常基本的),然后再寻找一些能为你完成工作的神奇代码。

其次,没有神奇的代码。我不是套接字编程人员,但在我看来,在客户端机器上运行的应用程序中,您需要硬编码服务器的名称。如果你不想这样做,编程的方式是只有你的服务器机器监听特定的端口,所有的客户端机器都会监听不同的端口。因此,局域网中的每台机器都可以枚举可用机器并首次建立/确定客户机服务器连接/关系。而且这种方法仍然非常难看,除非你正在编写病毒或其他东西。