Converting [NSNetservice addresses] to ip address string
Asked Answered
M

2

7

This question was already asked here: NSNetservice IP address

and here: iPhone: Bonjour NSNetService IP address and port

I've used both of those to get where I'm at now. My problem is the following method I have doesn't quite work:

- (NSString *)getStringFromAddressData:(NSData *)dataIn {
    struct sockaddr_in  *socketAddress = nil;
    NSString            *ipString = nil;

    socketAddress = (struct sockaddr_in *)[dataIn bytes];
    ipString = [NSString stringWithFormat: @"%s",
                inet_ntoa(socketAddress->sin_addr)];  ///problem here
    return ipString;
}

This gives a build error saying both "Dereferencing pointer to incomplete type" and "Implicit declaration of function 'inet_ntoa'" on the same line.

I've checked and I don't think I'm missing some type of framework to get inet_ntoa to work. Any idea what I'm doing wrong? thanks.

Malchy answered 24/2, 2010 at 17:3 Comment(1)
Acctually inet_ntoa is depreciated, use inet_ntop instead!Darrick
D
19

If you're getting implicit declaration of inet_ntoa, you probably need

 #include <arpa/inet.h>

(You can man inet_ntoa to get this info and read more about that function)

Devious answered 24/2, 2010 at 17:8 Comment(1)
That worked perfectly. Thank you very much for the speedy response!Malchy
C
3

FWIW I've posted an updated code snippet for handling NSNetService address resolution over at

iPhone: Bonjour NSNetService IP address and port.

Coretta answered 12/2, 2011 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.