Hello.
I have a problem with my program.
I created a socket with "kCFSocketReadCallBack.
My intention was to call the "acceptCallback" only when it receives a string to the socket.
Instead my program does not just accept the connection always goes into "startReceive" stop doing so and sometimes crash the program.
Can anybody help?
Thanks
readSocket = CFSocketCreateWithNative(
NULL,
fd,
kCFSocketReadCallBack,
AcceptCallback,
&context
);
static void AcceptCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
// Called by CFSocket when someone connects to our listening socket.
// This implementation just bounces the request up to Objective-C.
{
ServerVistaController * obj;
#pragma unused(address)
// assert(address == NULL);
assert(data != NULL);
obj = (ServerVistaController *) info;
assert(obj != nil);
#pragma unused(s)
assert(s == obj->listeningSocket);
if (type & kCFSocketAcceptCallBack){
[obj acceptConnection:*(int *)data];
}
if (type & kCFSocketAcceptCallBack){
[obj startReceive:*(int *)data];
}
}
-(void)startReceive:(int)fd
{
CFReadStreamRef readStream = NULL;
CFIndex bytes;
UInt8 buffer[MAXLENGTH];
CFStreamCreatePairWithSocket(
kCFAllocatorDefault,
fd,
&readStream,
NULL);
if(!readStream){
close(fd);
[self updateLabel:@"No readStream"];
}
CFReadStreamOpen(readStream);
[self updateLabel:@"OpenStream"];
bytes = CFReadStreamRead(
readStream,
buffer,
sizeof(buffer));
if (bytes < 0) {
[self updateLabel:(NSString*)buffer];
close(fd);
}
CFReadStreamClose(readStream);
}