Thank you for your quick response, Josh.
>From all that I've read, I knew UDP is connectionless, but I wasn't
sure about the appropriateness of a "listener".
Additionally, I haven't found any examples or articles about UDP
listeners. That's why I questioned whether it was appropriate or not.
Do I need two sockets, one to send and one to receive?
In the interest of brevity, I did not include source code in my
original posting. I've written two very stripped down functions to
create the two sockets. Addresses and ports are hard-coded in these
functions. No functional error checking, but I do report success or
failure on key function calls to the console. My CallBack function
consists of sending a string to the console (via NSLog). When the
CallBack function gets called, I'll worry about the fleshing out that
part...
In the listen socket, I've tried all of the following CallBack types:
- kCFSocketAcceptCallBack
- kCFSocketConnectCallBack
- kCFSocketDataCallBack
- kCFSocketReadCallBack
The CallBack function is never called.
/*
Socket - Send
*/
-(Boolean)setupSocketSend {
Boolean retval;
struct sockaddr_in addr;
// zero out the ADDRESS structure
memset(&addr, 0, sizeof(struct sockaddr_in));
// Low level address work
addr.sin_len = sizeof(struct sockaddr_in);
addr.sin_family = AF_INET;
// Address representation
addr.sin_addr.s_addr = inet_addr([@"192.168.1.110" UTF8String]);
// Port representation
addr.sin_port = htons(21075);
// Create the CFDataRef for the address data
addr_sig_send = CFDataCreate(kCFAllocatorDefault, (unsigned char
*)&addr, sizeof(struct sockaddr_in));
if (addr_sig_send) {
NSLog(@"CFDataCreate: addr_sig_send: SUCCESS");
retval = TRUE;
} else {
NSLog(@"CFDataCreate: addr_sig_send: FAILURE");
retval = FALSE;
}
// create the UDP socket
socket_send = CFSocketCreate(kCFAllocatorDefault, PF_INET,
SOCK_DGRAM, IPPROTO_UDP, kCFSocketNoCallBack, NULL, NULL);
if (socket_send) {
retval = TRUE;
NSLog(@"CreateSocket: send: SUCCESS");
} else {
retval = FALSE;
NSLog(@"CreateSocket: send: FAILURE");
}
// Add TX to run loop
CFRunLoopSourceRef source;
source = CFSocketCreateRunLoopSource(NULL, socket_send, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source,
kCFRunLoopDefaultMode);
CFRelease(source);
CFRelease(socket_send);
return retval;
}
/*
Socket - Listen
*/
-(Boolean)setupSocketListen {
Boolean retval;
struct sockaddr_in addr;
CFSocketError err = 0;
// Create the ADDRESS structure
memset(&addr, 0, sizeof(struct sockaddr_in));
// Low level address work
addr.sin_len = sizeof(struct sockaddr_in);
addr.sin_family = AF_INET;
// Address representation
addr.sin_addr.s_addr = inet_addr([@"192.168.1.100" UTF8String]);
// Port representation
addr.sin_port = htons(21076);
// Create the CFDataRef for the address
addr_sig_listen = CFDataCreate(kCFAllocatorDefault, (unsigned char
*)&addr, sizeof(struct sockaddr_in));
if (addr_sig_listen) {
NSLog(@"CFDataCreate: addr_sig_listen: SUCCESS");
retval = TRUE;
} else {
NSLog(@"CFDataCreate: addr_sig_listen: FAILURE");
retval = FALSE;
}
CFSocketContext ctxt = {0, self, NULL, NULL, NULL };
// create the UDP socket
socket_listen = CFSocketCreate(kCFAllocatorDefault, PF_INET,
SOCK_DGRAM, IPPROTO_UDP, kCFSocketReadCallBack, deviceTwoCallBack,
&ctxt);
if (socket_listen) {
retval = TRUE;
NSLog(@"CreateSocket: listen: SUCCESS");
} else {
retval = FALSE;
NSLog(@"CreateSocket: listen: FAILURE");
}
err = CFSocketSetAddress(socket_listen, addr_sig_listen);
switch (err) {
case kCFSocketSuccess:
retval = TRUE;
NSLog(@"Socket connect: SUCCESS");
break;
case kCFSocketError:
NSLog(@"Socket connect: ERROR");
break;
case kCFSocketTimeout: // Should not happen with a negative
value...
NSLog(@"Socket connect: TIMEOUT");
break;
default:
break;
}
// Add listener to run loop
CFRunLoopSourceRef source;
source = CFSocketCreateRunLoopSource(NULL, socket_listen, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source,
kCFRunLoopDefaultMode);
CFRelease(source);
CFRelease(socket_listen);
return retval;
}
/*
CallBack - Function
*/
void deviceTwoCallBack(CFSocketRef sockRef, CFSocketCallBackType
cbType, CFDataRef dataRef, const void * data, void * info) {
NSLog(@"XXX - deviceTwoCallBack called...");
}
|