IPHostEntry AddressList

J

Jon Berry

I wrote a little app to get the IP address of the local machine like so:

IPHostEntry local = Dns.GetHostEntry(hostName);
foreach (IPAddress ipaddress in local.AddressList)
{
Console.WriteLine(ipaddress);
}

When I tested this on my XP Machine I get an IP address but on my Vista
Machine
I'm getting an IP and bunch of Mac Addresses (at least that's what I think
they are).

fe80::D950:865b: blah, blah, blah

Is there a way to filter this so I only get IP addresses?

Thanks!
 
J

Jon Berry

Of course, the IPv6 addresses are real IP addresses, so it may be you
really do want to see those. Without knowing why you want the IP
addresses of your computer, it's hard to say for sure (the "ipconfig"
command line utility can do basically the same thing and more, so surely
the application you wrote is not literally needed :) ).

Pete

Ahh...so those are IPv6 addresses.

What I'm doing is creating an incoming socket and I'm binding to the IP
address.

When I tried to bind to the IPv6 address I got a some kind of socket
exception
that I wasn't handling and the app crashed.

I'm using AddressFamily.InterNetwork in the SocketConstructor. It looks
like
there is also AddressFamily.InterNetwork6 so I've got some work to do...

Considering this app will only be used on the local intranet do I need to
support IPv6?

Why doesn't my XP box show the IPv6 addresses? Because I don't have that
setup or XP doesn't support it?
 
J

Jon Berry

Don't do that. Binding to a specific IP address is usually not useful.
Just bind to the appropriate "Any" address.
Pete

I was originally using IPAddress.Any, which works, but I wanted
to give the user a choice in case they have multiple network cards.

Also, I need to be able to display the IP address because they have
to manually configure the client. If I use IPAddress.Any I think it
just returns 0.0.0.0 so I'm back to looping through the AddressList.

There could be an advantage to that - what happens if you use IPAddress.Any
and there is no network interface? Does it throw an exception?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top