[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

A Function to Get IP Address of a Domain

Posted on 2008-06-13
17
Medium Priority
?
650 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 200 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 1200 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 19

Assisted Solution

by:Stephen Manderson
Stephen Manderson earned 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 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