Solved

A Function to Get IP Address of a Domain

Posted on 2008-06-13
17
639 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now