A C program I would like to prompt the user to enter an ip a

A C# program, I would like to prompt the user to enter an ip address or host name. It then returns the ip address and the host name. In C#

Solution

Please find the required program and output below: Please find the comments against each line for the description:

using System.IO;
using System;
using System.Net;
using System.Net.Sockets;

class Program
{
static void Main()
{
Console.WriteLine (\"Please enter an IP address or hostname\");
string host = Console.ReadLine (); //read the ip address or hostname from the user
  
IPHostEntry hostEntry;

hostEntry= Dns.GetHostEntry(host); //get the hostEntry object, which has got the resolution details
  
//you might get more than one ip for a hostname since
//DNS supports more than one record
  
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0]; //get the ip address from the hostEntry object
string hn = hostEntry.HostName; //get the hostname from the hostEntry object   
Console.WriteLine(\"IP address : {0}\",ip); //print the details
Console.WriteLine(\"Host name : {0}\",hn);
}
}
}

-----------------------------------------------------

OUTPUT:

Please enter an IP address or hostname google.com IP address : 172.217.26.206 Host name : google.com   

A C# program, I would like to prompt the user to enter an ip address or host name. It then returns the ip address and the host name. In C#SolutionPlease find th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site