Getting UDP sockets to work
Getting UDP sockets to work
- Subject: Getting UDP sockets to work
- From: Liwei <email@hidden>
- Date: Sun, 10 Aug 2008 01:19:03 +0800
I'm trying to get a UDP socket working using CFSocket, but haven't
been successful.
Firstly, from all the examples I can find on the net, CFSocket still
uses most of native BSD sockets constants and structures such as
sockaddr_*, PF_INET etc. In fact, other than CFSocketCreate,
CFSocketWriteData, CFSocketCreateRunLoopSource and a few other
functions, the examples aren't very much different from how I see
native BSD sockets are done. So am I right to say that CFSocket is
just a thin wrapper around the native BSD sockets such that I can use
run loops? Is this considered "clean" since its not really a fully
CoreFoundation thing (unlike CFStream where everything you do has a
CFxxx function)? What are the potential pitfalls?
Secondly, the following code I have is not working:
void receiveData(CFSocketRef socket, CFSocketCallBackType type,
CFDataRef address, const void *data, void *info){
CFSocketSendData(socket, address, (CFDataRef)data, -1);
}
int main (int argc, const char * argv[]){
struct sockaddr_in sin_struct = {0, AF_INET, 5555, 0};
CFDataRef sin = CFDataCreate(NULL, (UInt8 *)&sin_struct,
sizeof(struct sockaddr_in));
CFSocketSignature sig = {PF_INET, SOCK_DGRAM, IPPROTO_UDP, sin};
CFSocketRef socket = CFSocketCreateWithSocketSignature(NULL, &sig,
kCFSocketDataCallBack, receiveData, NULL);
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, socket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRunLoopRun();
return 0;
}
The code should simply echo whatever it receives. However, running the
code and running "nc -u 127.0.0.1 5555" in terminal does not give me
anything, although "nc -u -l 5555" on another terminal gives me what I
enter.
Can someone explain to me what is wrong?
Or even better, explain how I can write a simple application that
listens and echoes on UDP multicast/broadcast (either would do, and
yes, I'm doing this in an isolated testing network)
Thanks!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden