-1

Get the server name and ip address in C# 2010

I want to get the IP address of the server. The following code comes from:

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());
        }
}

This code must know the name of the server computer.
AddressFamily in System.Net.IPAddress

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

Error ------------->Use of unassigned local variable 'i'

How can I get the name of the server computer?

4

3 回答 3

1

要获取主机名,您可以执行以下操作:

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,网络上的客户端只会加入一个不特定于服务器的已知多播地址。

多播示例

于 2013-07-31T11:19:35.153 回答
0

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

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

于 2013-08-01T09:30:37.020 回答
-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;
    }
于 2013-08-11T14:11:55.280 回答