Re: Correct way to determine machine's IP address in Cocoa
Re: Correct way to determine machine's IP address in Cocoa
- Subject: Re: Correct way to determine machine's IP address in Cocoa
- From: Jens Bauer <email@hidden>
- Date: Sun, 21 Apr 2002 10:30:04 +0200
Hi Jamie,
>
Does anyone know what the best/correct way to determine the IP address
>
of a given machine is?
The following snippet comes from the Macintosh Network Programming list:
(I've modified the code, as I believe there was an obvious bug in it).
This is for Carbon; if you want to go clean Cocoa, I don't have an exact
answer:
char buffer[32]; /* a buffer large enough to hold the
string "###.###.###.###\0" */
INetInterfaceInfo info;
if(kOTNoError == OTInetGetInterfaceInfo(&info, kDefaultInetInterface))
{
OTInetHostToString(info.fAddress, buffer); /* info.fAddress
holds the IP address in an InetHost structure */
}
This will give you the IP address of your machine.
Now, if you're behind a firewall, this will not match the IP address that
people will see from the outside, but *don't worry*!
The IP address that people see from the outside is the IP address of the
firewall, so you should only use the IP address that you got above.
On Sun, 21 Apr, 2002, Jamie Curmi <email@hidden> wrote:
>
I've tried a number of things with different amounts of success:
>
>
1) NSHost
>
Doing something like:
>
[[NSHost currentHost] address]
>
>
Seems to work sometimes, but sometimes it just returns 127.0.0.1
This may be correct. Are you sure you have a network connection when it
returns 127.0.0.1 (which is localhost) ?
>
3) An Apple employee hinted at something to do with the
>
SystemConfiguration framework and using the DynamicStore API's. I've
>
tried to find out about this framework, but I can find much
>
documentation, and what little I did find was not really helpful for
>
doing what I want to do.
MoreSCF! -You can download it here:
<
http://developer.apple.com/samplecode/Sample_Code/Networking/MoreSCF.htm>
Love,
Jens
--
Jens Bauer, Faster Software.
-Let's make the World better, shall we ?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.