Solved

A Function to Get IP Address of a Domain

Posted on 2008-06-13
17
640 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
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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Two different visual studio versions 3 22
Angular JS Route 3 47
Want to create a userform that looks like the image provided 10 44
Get Client IP on RDS - VB.NET 15 25
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

19 Experts available now in Live!

Get 1:1 Help Now