Solved

A Function to Get IP Address of a Domain

Posted on 2008-06-13
17
642 Views
Last Modified: 2013-12-17
Code example will do please, thanks no algorithms.

I need a C# or VB.NET code function to return the IP address of a domain.

For example if I have the domain named as www.mydomain.com or mydomain.co.uk
I will need the IP address returned. I will need to do this in a batch so please
keep that in mind when coding, possibly threaded, thanks.
0
Comment
Question by:netimpact
  • 7
  • 5
  • 3
  • +1
17 Comments
 
LVL 6

Assisted Solution

by:MuhammadAdil
MuhammadAdil earned 50 total points
ID: 21782207
public string GetDomainIP(string domain)
        {
            IPHostEntry objIPHostEntry;
            objIPHostEntry = Dns.Resolve(domain); // relax-solutions.com
            return objIPHostEntry.AddressList[0].ToString();
        }

Regards,
Adil Fazal
0
 

Author Comment

by:netimpact
ID: 21782505
It failed on ==> objIPHostEntry = Dns.Resolve(domain);

The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for

System.Net.Sockets.SocketException was unhandled
  ErrorCode=11004
  Message="The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for"
  Source="System"
  StackTrace:
       at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
       at System.Net.Dns.Resolve(String hostName)
       at ParseTextFileNS.MainForm.GetDomainIP(String domain) in F:\...\MainForm.vb:line 137
       at ParseTextFileNS.MainForm.Button1_Click(Object sender, EventArgs e) in F:\...\MainForm.vb:line 148
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at ParseTextFileNS.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
0
 
LVL 1

Accepted Solution

by:
CartoonHead earned 300 total points
ID: 21783116
This works fine in a WinForm... Framework 2.0
Pretty much the same as MuhammadAdil's code.
private void button1_Click(object sender, EventArgs e)
        {
            IPHostEntry ip = Dns.GetHostEntry("www.yahoo.com");
            this.Text = ip.AddressList[0].ToString();
        }

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 19

Assisted Solution

by:Stephen Manderson
Stephen Manderson earned 150 total points
ID: 21783211
This is the function I use for this in a vb app as cartoons and muhammeds pretty much the same. Tested working in vs2005 & vs2008


Imports System.Net
.....
.........
    Public Function GetIPofURL(ByVal URL As String) As String
        Dim GetIP As IPHostEntry = Dns.GetHostEntry(URL)
        Return GetIP.AddressList(0).ToString
    End Function

Open in new window

0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 21783259
Dont use http and www with domain name. Etc. Google.com, yahoo.com
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21783267
You can use the full address with www. with gethostentry also .resolve has been replaced by gethostentry
0
 
LVL 1

Expert Comment

by:CartoonHead
ID: 21783312
It works with localhost etc... So I guess in a local domain 'PCName' would also return OK.
Or have I got your question wrong?
0
 

Author Comment

by:netimpact
ID: 21783516
CartoonHead:
Thank you ,your code worked for me.

Quick question if you don't mind. Would like my WinApps to look like Office 2007 lookalike how do I do that? Any articles will be good.

I have seen a few new apps like that and I want to find out how get that look and feel, thanks.
0
 
LVL 1

Expert Comment

by:CartoonHead
ID: 21783675
I think that is for another question - Head down the WPF path though
0
 

Author Comment

by:netimpact
ID: 21785427
Thanks I have found what I am looking for it is called DotNetBar.
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21785443
Look at devexpress
0
 

Author Comment

by:netimpact
ID: 21785484
MrManderson:

Your code also worked.
0
 

Author Comment

by:netimpact
ID: 21785488
MrManderson:
Do you do dev on the side?
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21785499
I do a bit of everything :-)

Server, security, programming, databases etc.. Headache :P
0
 

Author Comment

by:netimpact
ID: 21785503
CartoonHead:
Do you do development on the side?
0
 

Author Comment

by:netimpact
ID: 21785628
MrManderson:

You have a website?
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21787254
Sort of :-) not finished it yet address details on my profile
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question