tags:

views:

660

answers:

3

I am receiving a successful connection to the server and i am in my callback function:

I am trying to get the name of the host and print that to my console:

if(theType == kCFSocketConnectCallBack){
        NSLog(@"Connection is accepted");
        CFSocketNativeHandle nativeSocket = CFSocketGetNative(theSocket);
        uint8_t name[SOCK_MAXADDRLEN];
        socklen_t namelen = sizeof(name);
        NSData *peer = nil;
        if (getpeername(nativeSocket, (struct sockaddr *)name, &namelen) == 0) {
            peer = [NSData dataWithBytes:name length:namelen];
            NSString *string = [[NSString alloc] initWithData:peer encoding:NSUTF8StringEncoding];
            NSLog(@"IP adress of connected peer: %@", string);
        } 

When i run the application in the debug mode i can see the IP address value assigned to name , so it's successful in getting the name , each value is uint8_t.. The peer length is showing me 16; My problem converting it to NSData then NSString...

output: 2010-01-31 13:57:58.671 IP adress of connected peer: (null)

My string is being output as NULL,

Any advise is appreciated, thanks....

A: 

First, check to make sure that peer contains an instance of NSData that is non-zero in length.

If it does, then the most likely problem is that the data is not properly of the specified encoding causing NSString to fail the encoding and return a nil string. If it is an encoding issue, there is API on NSString for doing lossy encodings. Even if lossy isn't acceptable in your final solution, going that route can be quite helpful to figuring out what is going wrong.

That assumes by NULL you really mean nil. It would be helpful to paste the exact output of the log line.

bbum
my peer length is 16I updated my question to clarify things a little more ....
Unis
+2  A: 

getpeername() doesn't return the host name of the remote side; it returns the address:

$ man getpeername

...

DESCRIPTION
     The getpeername() function returns the address of the peer connected to
     the specified socket.

You want getnameinfo():

$ man getnameinfo

...

DESCRIPTION
     The getnameinfo() function is used to convert a sockaddr structure to a
     pair of host name and service strings.  It is a replacement for and pro-
     vides more flexibility than the gethostbyaddr(3) and getservbyport(3)
     functions and is the converse of the getaddrinfo(3) function.

or gethostbyaddr():

$ man gethostbyaddr

...

DESCRIPTION
     The getaddrinfo(3) and getnameinfo(3) functions are preferred over the
     gethostbyname(), gethostbyname2(), and gethostbyaddr() functions.

     The gethostbyname(), gethostbyname2() and gethostbyaddr() functions each
     return a pointer to an object with the following structure describing an
     internet host referenced by name or by address, respectively.
Dewayne Christensen
Thanks for clarification, however i am getting the right information doing this , but i can't convert the peer(NSData) to NSString then output the value....
Unis
You can't print it because you're trying to create an NSString* out of non-c-string data. If you want to print it, then the call you are using is not giving you "the right information" for your purpose.
smorgan
+1  A: 

sockaddr is a struct, not just a typedef for a character array. You need to pass getpeername the address to an actual sockaddr struct, and then build the string from the sa_data field of that struct--and that's assuming that sa_data is actually a string for your address type, which the documentation doesn't suggest you can actually count on. As another answer says, this is not the call you want if your goal is to get a string representation to print out.

(Also, you don't need an NSData at all to go from a character array to an NSString; you can just use -[NSString initWithBytes:length:encoding:])

smorgan