1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Best book for Objective-C networking?

Discussion in 'Mac Programming' started by dylanweber, Mar 27, 2012.

  1. macrumors newbie

    #1
    I've already read "Programming in Objective-C 2.0; Second Edition" by Stephen G. Kochan, and I believe I have absorbed all the information from it.

    After a while of programming with a friend of mine, we tried using online resources to begin making small little apps that would communicate with a server. Once that worked, we wanted to make our program secure using SSL. We couldn't find any information that helped, and we're stuck. Can anyone suggest an Objective C book for information on networking or CFNetwork?
     
  2. macrumors 603

    #2
    What classes or functions are you using now to do the server communication?


    Did you look at this doc: URL Loading System Programming Guide

    Or see these:
    http://stackoverflow.com/questions/3768893/asihttprequest-https-with-ssl
    http://allseeing-i.com/ASIHTTPRequest/

    Depending on what you're using now, maybe all you need is to use an HTTPS url instead of an HTTP one.
     
  3. macrumors 68000

    #3
    There are two chapters on networking (and run loops) in Advanced Mac OS X programming.

    You could also check out Beej's network programming guide on the web, it's free.

    But you'll probably want to just use some wrapper classes. It would be hard to write an SSL implementation from scratch.
     
  4. macrumors newbie

    #4
    I think you're implying I'm using an HTTP protocol with URLs. I am using CFNetwork sockets, HTTP and HTTPS has nothing to do with what I've been doing.

    ----------

    Is this the book?

    Also, I've used Beej's guide as a source many times. He never mentions any specifics on SSL.
     
  5. macrumors 603

    #5
    Then please explain exactly what you've been doing, so we know what the context is for your expected answer.

    If you're doing low-level sockets, then you have to implement TLS yourself, or find a library that does it for you. What have you tried? What have you searched for?

    For example, the search terms cfsocket tls show CocoaAsyncSocket in the first page of results. Have you looked at that?
     
  6. macrumors 68000

    #6
    That is the book. You'll want to check out chapters 15 and 16. It doesn't cover SSL, however.
     
  7. macrumors newbie

    #7
    Right now, I've been trying to make a server software that communicates with telnet. We had some non-SSL working examples earlier, and I can't seem to find them in my documents folder, but here is what I have now:

    Code:
    static void ServerAcceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
    //	Server *server = (__bridge Server *)info;
    	printf("ServerAcceptCallBack called.\n");
    	if (kCFSocketAcceptCallBack == type) { 
            // for an AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
            CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
            uint8_t name[SOCK_MAXADDRLEN];
            socklen_t namelen = sizeof(name);
            NSData *peer = nil;
            if (getpeername(nativeSocketHandle, (struct sockaddr *)name, &namelen) == 0) {
                peer = [NSData dataWithBytes:name length:namelen];
            }
            CFReadStreamRef readStream = NULL;
    		CFWriteStreamRef writeStream = NULL;
            CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &readStream, &writeStream);
            if (readStream && writeStream) {
                CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
                CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    			SocketThread *thread = [[SocketThread alloc] init];
    			[thread setupSocketwithReadStream:readStream andwithWriteStream:writeStream];
            } else {
                close(nativeSocketHandle);
            }
            if (readStream) CFRelease(readStream);
            if (writeStream) CFRelease(writeStream);
        }
    }
    
    @implementation Server
    
    -(BOOL)setupServer:(int)port {
    	CFSocketContext socketCtxt = {0, (__bridge void *)self, NULL, NULL, NULL};
        CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, (CFSocketCallBack)&ServerAcceptCallBack, &socketCtxt);
    	printf("Initiating Server Setup...\n");
        if (socket == NULL) {
            if (socket) CFRelease(socket);
            socket = NULL;
            return NO;
        }
    	
        int yes = 1;
        setsockopt(CFSocketGetNative(socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
    	
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_len = sizeof(addr);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
        NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)];
    	
        if (CFSocketSetAddress(socket, (__bridge CFDataRef)address) != kCFSocketSuccess) {
            if (socket) CFRelease(socket);
            socket = NULL;
            return NO;
        }
        printf("Server SHOULD be setup...\n");
    	return YES;
    }
    
    @end
     

Share This Page