iOS ARC build - find local interface after acceptOnPort

52 views
Skip to first unread message

whitehexagon

unread,
May 5, 2012, 3:26:19 AM5/5/12
to CocoaAsyncSocket
Once my server is created I want to email the server details. However
looking at the code logic on the above method, there doesn't seem to
be any way to achieve this. The first time I have a valid localHost
is after the first connect...

So is the only way to achieve this to do a dummy local connect to
0.0.0.0:port?

btw thanks for 'encouraging' me to go with ARC :) and a big thanks for
a great lib, been using it for over a year in a number of my apps.

whitehexagon

unread,
May 5, 2012, 12:19:04 PM5/5/12
to CocoaAsyncSocket
Actually I realize now this is a more generic question of how do I
know my LAN IP address in iOS, sounds like I'm gonna need something
based around getifaddrs. Sorry for the noise. Peter

Joe Francia

unread,
May 5, 2012, 7:37:59 PM5/5/12
to cocoaasy...@googlegroups.com

getifaddrs() is indeed the ticket.  This will print your hostname and en* IP address(es).  You can modify it to do something else with them, if you'd like.


#include <arpa/inet.h>

#include <sys/socket.h>

#include <netdb.h>

#include <ifaddrs.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>


-(void)printIPAddresses {

    struct ifaddrs *ifaddr, *ifa;

    int family, s;

    char host[NI_MAXHOST];

    char hostname[NI_MAXHOST];

    

    if (getifaddrs(&ifaddr) == -1) {

        perror("getifaddrs");

    }

    

    if(!gethostname(hostname, NI_MAXHOST)) {

        printf("Hostname: %s\n", hostname);

    }

    

    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {

        if (ifa->ifa_addr == NULL)

            continue;

        

        family = ifa->ifa_addr->sa_family;

        

        if (family == AF_INET || family == AF_INET6) {

            s = getnameinfo(ifa->ifa_addr,

                            (family == AF_INET) ? sizeof(struct sockaddr_in) :

                            sizeof(struct sockaddr_in6),

                            host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);

            if (s != 0) {

                DDLogError(@"getnameinfo() failed: %s", gai_strerror(s));

            }

            

            //only print the ethernet/wifi interface (en*), not loopback, 3g or vpn

            if (strncmp(ifa->ifa_name, "en", 2) == 0) {

                printf("address: %s\n", host);

            }

        }

    }

    

    freeifaddrs(ifaddr);

}


-- 
Joe Francia


--
You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
To post to this group, send email to cocoaasy...@googlegroups.com.
To unsubscribe from this group, send email to cocoaasyncsock...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cocoaasyncsocket?hl=en.

whitehexagon

unread,
May 6, 2012, 7:06:50 AM5/6/12
to CocoaAsyncSocket
Thanks for that, I can make some progress again :)
Reply all
Reply to author
Forward
0 new messages