Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 1, 2008, 08:41 PM   #1
likenoother
macrumors newbie
 
Join Date: Oct 2008
CFSocket Callback not working....Please Help Me

I am trying to receive broadcast on UDP. I have tried everything possible but i dont receive any callback. but i see the socket open on lsof. it shows my socket with the port number 15598. but for some reason dealWithData does not get called. am i missing something here i am very new to CFsockets. Please help

<Code>
static void dealWithData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
printf("received %d bytes from socket %d\n", CFDataGetLength((CFDataRef)data), CFSocketGetNative(s));
}

static void receiveData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
// CFSocketSendData(socket, address, (CFDataRef)data, 0.0);
printf("Hello World");
}


static void SetSocketBlockingEnabled(int sock, bool blocking)
{
int flags = fcntl(sock, F_GETFL, 0);
flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK);
fcntl(sock, F_SETFL, flags);
}

-(void) searchHosts
{
CFSocketRef searchsocket = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack, (CFSocketCallBack)&dealWithData, NULL);
int fd = -1;
if ( searchsocket == NULL) {
NSLog(@"CfSocketCreate Failed");
}else{
if( searchsocket ) {
fd = CFSocketGetNative(searchsocket);
if (fd == -1) {

NSLog(@"CfSocketGetnative Failed");}
else{
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&yes, sizeof(yes));
//SetSocketBlockingEnabled(fd, true);
}
}
}

struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(15598); //port
//inet_aton("255.255.255.255", &addr.sin_addr);
//addr.sin_addr.s_addr = htonl(INADDR_ANY);

//bind(fd, (struct sockaddr *)&addr, sizeof(addr));

//struct sockaddr_in a = {0, AF_INET, 1234, 0};
CFDataRef searchAddress = CFDataCreate(NULL, (UInt8 *)&addr, sizeof(struct sockaddr_in));

CFSocketSetAddress(searchsocket, searchAddress);

//CFSocketSetAddress(searchsocket, searchAddress);

CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, searchsocket, 0);

if(source == NULL)
{
NSLog(@"Sorry could not created loop");
}

CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode);

//CFRelease(source);
//CFRelease(searchsocket);
CFRunLoopRun();
}
</Code>

Please help i am really stuck here.
likenoother is offline   0 Reply With Quote
Old Dec 9, 2010, 12:00 AM   #2
bluegene
macrumors newbie
 
Join Date: Dec 2010
i think you should check the return value of the function CFSocketSetAddress(searchsocket, searchAddress),ensure weather it returns success or other errors.
if it returns error,i think the callback would not be called.

Quote:
Originally Posted by likenoother View Post
I am trying to receive broadcast on UDP. I have tried everything possible but i dont receive any callback. but i see the socket open on lsof. it shows my socket with the port number 15598. but for some reason dealWithData does not get called. am i missing something here i am very new to CFsockets. Please help

<Code>
static void dealWithData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
printf("received %d bytes from socket %d\n", CFDataGetLength((CFDataRef)data), CFSocketGetNative(s));
}

static void receiveData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
// CFSocketSendData(socket, address, (CFDataRef)data, 0.0);
printf("Hello World");
}


static void SetSocketBlockingEnabled(int sock, bool blocking)
{
int flags = fcntl(sock, F_GETFL, 0);
flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK);
fcntl(sock, F_SETFL, flags);
}

-(void) searchHosts
{
CFSocketRef searchsocket = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack, (CFSocketCallBack)&dealWithData, NULL);
int fd = -1;
if ( searchsocket == NULL) {
NSLog(@"CfSocketCreate Failed");
}else{
if( searchsocket ) {
fd = CFSocketGetNative(searchsocket);
if (fd == -1) {

NSLog(@"CfSocketGetnative Failed");}
else{
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&yes, sizeof(yes));
//SetSocketBlockingEnabled(fd, true);
}
}
}

struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(15598); //port
//inet_aton("255.255.255.255", &addr.sin_addr);
//addr.sin_addr.s_addr = htonl(INADDR_ANY);

//bind(fd, (struct sockaddr *)&addr, sizeof(addr));

//struct sockaddr_in a = {0, AF_INET, 1234, 0};
CFDataRef searchAddress = CFDataCreate(NULL, (UInt8 *)&addr, sizeof(struct sockaddr_in));

CFSocketSetAddress(searchsocket, searchAddress);

//CFSocketSetAddress(searchsocket, searchAddress);

CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, searchsocket, 0);

if(source == NULL)
{
NSLog(@"Sorry could not created loop");
}

CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode);

//CFRelease(source);
//CFRelease(searchsocket);
CFRunLoopRun();
}
</Code>

Please help i am really stuck here.
bluegene is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Apple store not working, please help me StefanCourteaux Buying Tips and Advice 11 Jan 30, 2013 07:50 AM
Anyone out there a Blu Ray pro that can help me get BD-Live to work? deannnnn Community Discussion 0 Feb 21, 2011 11:04 PM
My iMac stopped working. Please help me :) lisamel Mac Basics and Help 9 Oct 30, 2009 09:53 AM
Garageband isn't working. Please help me out! kenny1811 Mac Applications and Mac App Store 1 Apr 14, 2009 11:24 AM
Dock does not appear and dashboard, expose, spaces will not work. PLEASE HELP gallinger OS X 3 Jan 17, 2008 01:57 AM


All times are GMT -5. The time now is 06:19 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC