Re: local host IP address on X
Re: local host IP address on X
- Subject: Re: local host IP address on X
- From: Quinn <email@hidden>
- Date: Tue, 16 Jul 2002 10:32:17 +0100
At 20:02 -0700 15/7/02, Kevin Parichan wrote:
I'm porting some Windows WinSock code to the Mac. The 8/9 version works
fine via a 3rd party sockets library, but the X version is having
problems. I'm trying to determine the local host IP address. The original
code is using gethostname, gethostbyname, then inet_ntoa. What's coming
back is a list of one item with the address 127.0.0.1. This obviously
doesn't match what is in the Internet Connect window and not what I
expected. If I use OTInetGetInterfaceInfo it returns the same thing at
the Internet Connect window. I looked at the Stevens book, and other than
using uname instead of gethostname, is basically doing the same thing.
I'm new to the sockets stuff, so what is it that I'm not understanding or
doing wrong?
The problem here is that these standard sockets calls aren't really
well suited to a dynamic TCP/IP environment. Because Mac OS X has
lots of code that assumes that these calls do something meaningful,
they've been hardwired to always return the loopback address. That
way UNIX programs that assume a single fixed IP address continue to
limp along.
The best fix is to use the System Configuration framework APIs to
build an IP address list, and also to be notified when that list
changes. Alternatively, you can use the get_ifi_info routine (on
page 434 of Stevens) to get the full list of IP addresses and choose
amongst them.
btw There's a lot of background to this issue in TN 1145 "Living in a
Dynamic TCP/IP Environment". I keep meaning to update that for Mac
OS X and SCF, but I haven't quite finished the job yet. Still, it
makes interesting reading even from the Mac OS 9 perspective.
<
http://developer.apple.com/technotes/tn/tn1145.html>
S+E
--
Quinn "The Eskimo!" <
http://www.apple.com/developer/>
Apple Developer Technical Support * Networking, Communications, Hardware
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.