Skip navigation.
 
mlUsing NSFileHandleConnectionAcceptedNotification for a server process
FROM : Matt Mashyna
DATE : Fri Feb 08 22:56:20 2008

I'm trying to build a simple server and I'm using NSFileHandle to read 
the incoming requests. It works, more or less, but what I have run 
into is strange. In my call back when I read from the file handle I 
always only get the first 510 bytes. I tried to use

int maxBuf=4096;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&maxBuf, sizeof(maxBuf));

before binding and registering for 
NSFileHandleConnectionAcceptedNotification but I still only get 510 
bytes. I tried other larger and smaller values but the result is 
always the same. I could keep queuing up calls with 
NSFileHandleReadCompletionNotification but how will I know when I'm 
done getting the data I am expecting ? I'm expecting a complete xml 
record that I'm going to read process and reply to. I could go back 
and retread one written in C but that wouldn't be any fun.

Any ideas how I can get it to feed me more upfront ?
Matt

snippet:

        int fd = -1;
        CFSocketRef socket;
        socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, 
SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL);
        if( socket ) {
            fd = CFSocketGetNative(socket);
            int yes = 1;
           int maxBuf = 4096;
           int err = 0;
            err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void 
*)&yes, sizeof(yes));
            err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void 
*)&maxBuf, sizeof(maxBuf));

            struct sockaddr_in addr4;
            memset(&addr4, 0, sizeof(addr4));
            addr4.sin_len = sizeof(addr4);
            addr4.sin_family = AF_INET;
            addr4.sin_port = htons(port);
            addr4.sin_addr.s_addr = htonl(INADDR_ANY);
            NSData *address4 = [NSData dataWithBytes:&addr4 
length:sizeof(addr4)];
            if (kCFSocketSuccess != CFSocketSetAddress(socket, 
(CFDataRef)address4)) {
                NSLog(@"Could not bind to address");
            }
        } else {
            NSLog(@"No server socket");
        }

        fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
                                                    closeOnDealloc:YES];

        NSNotificationCenter *nc = [NSNotificationCenter 
defaultCenter];
        [nc addObserver:self
                selector:@selector(newConnection:)
                    name:NSFileHandleConnectionAcceptedNotification
                  object:nil];

        [fileHandle acceptConnectionInBackgroundAndNotify];
    }

Related mailsAuthorDate
mlUsing NSFileHandleConnectionAcceptedNotification for a server process Matt Mashyna Feb 8, 22:56
mlRe: Using NSFileHandleConnectionAcceptedNotification for a server process Jens Alfke Feb 8, 23:07
mlRe: Using NSFileHandleConnectionAcceptedNotification for a server process Matt Mashyna Feb 9, 02:14
mlRe: Using NSFileHandleConnectionAcceptedNotification for a server process Jens Alfke Feb 10, 01:14