Get the server name and ip address in C# 2010

2020-01-20 01:39发布

问题:

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?

回答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;
    }


回答2:

To get the host name you can do the following:

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

If you want the hostname and (first IPv4) IP of your computer use the following:

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

The name and the ip will hold the info for the local computer.

The server could then send out the ip via a udp multicast and the client on the network would just join a known multicast address that is not specific to the server.

multicast example.



回答3:

First of all, you need to figure out for yourself that error(unassigned local variable) and learn why it is coming(it is very basic), before looking for some magical code that will do the job for you.

And secondly, there is no magical code. I am no socket programmer but it seems to me that in your application running on the client machines, you need to hardcode the name of your server. If you don't want to do that, program in such a way that only your server machine will listen on a particular port and all client machines will listen on a different port. Thus, each machine in the LAN can enumerate over the available machines and establish/determine the client server connection/relation for the first time. and that approach is still very ugly unless you are writing a virus or something.