Using NSFileHandleConnectionAcceptedNotification for a server process
Using NSFileHandleConnectionAcceptedNotification for a server process
- Subject: Using NSFileHandleConnectionAcceptedNotification for a server process
- From: Matt Mashyna <email@hidden>
- Date: Fri, 8 Feb 2008 16:56:20 -0500
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];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden