• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: INADDR_ANY not wokring
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: INADDR_ANY not wokring


  • Subject: Re: INADDR_ANY not wokring
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 20 May 2008 17:50:28 -0700


On 20 May '08, at 5:22 PM, Kenneth Perry Hough wrote:

when I use INADDR_ANY to get my local address I get 0.0.0.0 instead of what my ip address is on en0 or en1, depending on the interface I am using.

INADDR_ANY is a constant, not a function. It's hardcoded to 0.0.0.0. From <netinet/in.h>:
#define INADDR_ANY (u_int32_t)0x00000000
It's not going to return your actual address on any Unix platform.


If you want to look up the IPv4 address of the primary interface, here's a code snippet I've used that does that:

// getifaddrs returns a linked list of interface entries;
// find the first active non-loopback interface with IPv4:
UInt32 address = 0;
struct ifaddrs *interfaces;
if( getifaddrs(&interfaces) == 0 ) {
struct ifaddrs *interface;
for( interface=interfaces; interface; interface=interface- >ifa_next ) {
if( (interface->ifa_flags & IFF_UP) && ! (interface- >ifa_flags & IFF_LOOPBACK) ) {
const struct sockaddr_in *addr = (const struct sockaddr_in*) interface->ifa_addr;
if( addr && addr->sin_family==AF_INET ) {
address = addr->sin_addr.s_addr;
break;
}
}
}
freeifaddrs(interfaces);
}
return address;


I probably got this from someone else when I asked how to do this; I'm not an expert on how it works.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: INADDR_ANY not wokring
      • From: Quinn <email@hidden>
References: 
 >INADDR_ANY not wokring (From: Kenneth Perry Hough <email@hidden>)

  • Prev by Date: Re: INADDR_ANY not wokring
  • Next by Date: Obtaining local host name
  • Previous by thread: Re: INADDR_ANY not wokring
  • Next by thread: Re: INADDR_ANY not wokring
  • Index(es):
    • Date
    • Thread