How to find the IP address of one system


In This article we see how to find IP Address of one system,This is useful for Network programming Developer. so lets see step by step
  1. Step- open VS studio 2008/2010 ----Console Application
     
  2. First add network related Namespace like

    Using System.Net;

    And Using System.Net,NetworkInformation;
     
  3. And write follow code

    {
      string hostname = Dns.GetHostName();
                    Console.WriteLine(hostname);
                    IPAddress[] ipaddress = Dns.GetHostAddresses(hostname);
     
                    Console.WriteLine(ipaddress[1]);

    }

    Above code is to find IP address of one system if you want communicate between two system throw IP address in LAN then use follow code

    {
                   Ping pingSender = new Ping();
                    PingOptions options = new PingOptions();

                    options.DontFragment = true;

                    // Create a buffer of 32 bytes of data to be transmitted.
                    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                    byte[] buffer = Encoding.ASCII.GetBytes(data);
                    int timeout = 120;
                    PingReply reply = pingSender.Send(ipaddress[1], timeout, buffer, options);
                    if (reply.Status == IPStatus.Success)
                    {
                        Console.WriteLine("Address: {0}", reply.Address.ToString());
                        Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                        Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                        Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                        Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                    }

    }
     

  4. Run this code and give command ping and checked whether communication is going or not.
Ebook Download
View all
FileInfo in C#
Read by 9.7k people
Download Now!
Learn
View all