Connecting a CFSocket to the Loopback
Connecting a CFSocket to the Loopback
- Subject: Connecting a CFSocket to the Loopback
- From: Scott Thompson <email@hidden>
- Date: Sat, 31 May 2003 01:21:11 -0500
Greetings folks. I'm a rank beginner when it comes to network
programming so I beg of you to bear with me...
I'm trying to write an application that listens to the UDP broadcasts
of X-Plane (<
http://www.x-plane.org>). X-Plane allows me to identify
an address and a port to which it will broadcast it's UDP datagrams
full of interesting data. A program that is interested in getting
those data can simply tune in to that port. For the sake of
experimentation I decided to have X-Plane send it's information to the
loopback interface (127.0.0.1) and port 49001 so that I could run
X-Plane and my silly application both on the same machine.
I took my first steps into the brave new world (to me at least) of
network programming and cobbled together some CFSocket code (see below)
by watching WWDC sessions, reading a lot of documentation, and looking
at the sample code on my system.
In the code, I first try to create a socket address for the loopback
address and port 49001. I then create a socket that on which I expect
to listen for the UDP datagrams. I setup a callback, grab a runloop
source, and schedule it on the current runloop. Finally, I try to
attach the socket to my address using CFSocketSetAddress.
Things proceed just fine up to the call to CFSocketSetAddress. At that
point I run into the crux of the matter I am concerned about.
When setting up my address, if I ask that the socket listen on any
address (INADDR_ANY) then things proceed just fine and I can get the
data I am interested in. If, however, back when I set up my address I
use the loopback address (INADDR_LOOPBACK), my call to
CFSocketSetAddress will fail and return an error.
In the grand scheme of things, this really doesn't matter... I mean I
got my code to work, right? But it peaks my curiosity nonetheless.
Would someone please tell me why my code fails if I explicitly ask for
the loopback address?
Scott
P.S. If you have any other comments about ways my code could be
improved, I welcome constructive criticism.
----- Code Snippet follows -----
struct sockaddr_in myAddress;
myAddress.sin_family = AF_INET;
myAddress.sin_port = htons(49001);
#if 0
myAddress.sin_addr.s_addr = htonl(INADDR_ANY);
#else
myAddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
#endif
CFRunLoopSourceRef runLoopSource = NULL;
CFSocketRef mySocket = CFSocketCreate(
NULL,
PF_INET,
SOCK_DGRAM,
IPPROTO_UDP,
kCFSocketDataCallBack,
SocketCallback,
NULL);
if(NULL != mySocket) {
runLoopSource = CFSocketCreateRunLoopSource(NULL, mySocket, 0);
if(NULL != runLoopSource) {
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopCommonModes);
CFDataRef addressData = CFDataCreateWithBytesNoCopy(
NULL,
(UInt8 *)&myAddress,
sizeof(struct sockaddr_in),
kCFAllocatorNull);
CFSocketError error = CFSocketSetAddress(mySocket,
addressData);
if(kCFSocketSuccess != error) {
NSBeep();
}
CFRelease(addressData);
addressData = NULL;
}
}
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.