|
|
#1 |
|
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. |
|
|
|
0
|
|
|
#2 | |
|
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:
|
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 09:01 PM.







Linear Mode

