How to get the IP address of the server on which my C# application is running on?

前端 未结 26 1959
天命终不由人
天命终不由人 2020-11-22 06:01

I am running a server, and I want to display my own IP address.

What is the syntax for getting the computer\'s own (if possible, external) IP address?

Someon

26条回答
  •  终归单人心
    2020-11-22 06:51

    namespace NKUtilities 
    {
        using System;
        using System.Net;
        using System.Net.Sockets;
    
        public class DNSUtility
        {
            public static int Main(string [] args)
            {
                string strHostName = "";
                try {
    
                    if(args.Length == 0)
                    {
                        // Getting Ip address of local machine...
                        // First get the host name of local machine.
                        strHostName = Dns.GetHostName();
                        Console.WriteLine ("Local Machine's Host Name: " +  strHostName);
                    }
                    else
                    {
                        // Otherwise, get the IP address of the host provided on the command line.
                        strHostName = args[0];
                    }
    
                    // Then using host name, get the IP address list..
                    IPHostEntry ipEntry = Dns.GetHostEntry (strHostName);
                    IPAddress [] addr = ipEntry.AddressList;
    
                    for(int i = 0; i < addr.Length; i++)
                    {
                        Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
                    }
                    return 0;
    
                } 
                catch(SocketException se) 
                {
                    Console.WriteLine("{0} ({1})", se.Message, strHostName);
                    return -1;
                } 
                catch(Exception ex) 
                {
                    Console.WriteLine("Error: {0}.", ex.Message);
                    return -1;
                }
            }
        }
    }
    

    Look here for details.

    You have to remember your computer can have more than one IP (actually it always does) - so which one are you after.

提交回复
热议问题