Showing posts with label server implementation. Show all posts
Showing posts with label server implementation. Show all posts

How to fix naive server implementation with CFSocket to allow multiple connections

How to fix naive server implementation with CFSocket to allow multiple connections By Naim Hashmi

- (void) _acceptConnection:(int)fd
{
    int     junk;

// If we already have a connection, reject this new one.  This is one of the
// big simplifying assumptions in this code.  A real server should handle
// multiple simultaneous connections.

    if ( self.isReceiving ) {
        junk = close(fd);
        assert(junk == 0);
    } else {
        [self _startReceive:fd];
    }
}


// Called by CFSocket when someone connects to our listening socket.
// This implementation just bounces the request up to Objective-C.
static void AcceptCallback(CFSocketRef s,
                           CFSocketCallBackType type,
                           CFDataRef address,
                           const void *data,
                           void *info)

{
    ReceiveServer *  obj;

    assert(type == kCFSocketAcceptCallBack);

    assert(data != NULL);

    obj = (ReceiveServer *) info;
    assert(obj != nil);

    assert(s == obj->_listeningSocket);


    [obj _acceptConnection:*(int *)data];
}