Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have the following code which gets the local ip address which I set up so I have something to work on:

VB
Dim ipEntry As IPHostEntry = Dns.GetHostByName(Environment.MachineName)
        Dim IpAddr As IPAddress() = ipEntry.AddressList
        Dim i As Integer

        TextBox3.Text = IpAddr(0).ToString()


The above code displays the local ip address in a textbox but my question is how do I change the code so vb gets the ip address of a website entered in a textbox called 'textbox2'?

Thanks in advance to any helpers
Posted
Updated 19-Aug-10 2:23am

Instead of using Environment.MachineName, use TextBox2.Text.
 
Share this answer
 
Comments
hamza_786 19-Aug-10 9:48am    
if i change that to 'textbox2.text' then i get an error saying socket exception was unhandled whilst highlighting 'ipEntry As IPHostEntry = Dns.GetHostByName(TextBox1.Text)'

do you have solution?
Gregory Gadow 19-Aug-10 12:41pm    
If TextBox2 holds the domain that you are looking up, then why would you be passing TextBox1.Text? Also, if the value passed to GetHostByName is not a valid, registered domain, you will get a socket exception: make sure that you are entering a valid value for testing.
First. Add try...catch. An exception text may help you.
 
Share this answer
 
Comments
hamza_786 19-Aug-10 9:54am    
no luck mate.can you copy my code from above and tell me how you are saying my code should look like please. i have it set up as below:

Try
Dim ipEntry As IPHostEntry = Dns.GetHostByName(TextBox1.Text)
Dim IpAddr As IPAddress() = ipEntry.AddressList
Dim i As Integer

TextBox3.Text = IpAddr(0).ToString()

Dim hostname As IPHostEntry = Dns.GetHostEntry(TextBox1.Text)
Dim ip As IPAddress() = hostname.AddressList

TextBox3.Text = ip(0).ToString()

Catch ex As Exception

End Try
Try
	Dim ipEntry As IPHostEntry = Dns.GetHostByName(TextBox1.Text)
	Dim IpAddr As IPAddress() = ipEntry.AddressList
	Dim i As Integer

	TextBox3.Text = IpAddr(0).ToString()

	Dim hostname As IPHostEntry = Dns.GetHostEntry(TextBox1.Text)
	Dim ip As IPAddress() = hostname.AddressList

	TextBox3.Text = ip(0).ToString()

Catch ex As Exception
	TextBox3.Text = ex.ToString()
End Try


Then you'll see what caused an exception I suppose. I am not VB.NET coder, so I cannot test the code.
 
Share this answer
 
Comments
hamza_786 19-Aug-10 10:04am    
thank you that problem is fixed but it is still giving me my local ip address back.

there is something wrong in these line, i think:

TextBox3.Text = ip(0).ToString()

Catch ex As Exception
TextBox3.Text = ex.ToString()
Nyarost 19-Aug-10 10:10am    
Try tracing line by line. I am sure you'll see content of all variables ;)
hamza_786 19-Aug-10 10:11am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900