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

Similar Threads
thread Thread Starter Forum Replies Last Post
WiFi Sync Not Working iOS7, Working iOS6 kramden88 iOS 7 0 Jan 2, 2014 02:45 PM
iPhone: iPhone 5 / iPad 3 with Airport Extreme and WPA2 not working, WPA working ManuCH iOS 6 7 Aug 13, 2013 08:20 PM
iOS 7 b4 and x-callback-url 123321123321 iOS 7 0 Aug 5, 2013 05:15 PM
General: The List of iOS 6 Compatible Jailbreak Tweaks (Working/Non Working) HeyGreggie Jailbreaks and iOS Hacks 363 Feb 26, 2013 06:43 PM
how to pass multiple uitableviews to the tableView callback method? johnmerlino iPhone/iPad Programming 3 Jun 7, 2012 02:20 PM

Forum Jump

All times are GMT -5. The time now is 11:35 AM.

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