开发者

Convert NSData to NSString

开发者 https://www.devze.com 2022-12-18 21:18 出处:网络
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:

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 n开发者_开发问答ame , 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....


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.


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:])


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号