Re: Receiving UDP broadcasts
Re: Receiving UDP broadcasts
- Subject: Re: Receiving UDP broadcasts
- From: Scott Thompson <email@hidden>
- Date: Sat, 7 Jun 2003 19:49:52 -0500
FWIW I have the following code that accepts UDP packets just fine. It
uses CFSocket for the entire process instead of creating a native
socket and running it.
{
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc]
init];
struct sockaddr_in myAddress;
myAddress.sin_family = AF_INET;
myAddress.sin_port = htons(49001);
#if 1
myAddress.sin_addr.s_addr = htonl(INADDR_ANY);
#else
myAddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
#endif
CFSocketContext socketContext;
socketContext.version = 0;
socketContext.info = (void *) self;
socketContext.retain = XPlaneDataRetainCallback;
socketContext.release = XPlaneDataReleaseCallback;
socketContext.copyDescription = XPlaneDataCopyDescriptionCallback;
CFRunLoopSourceRef runLoopSource = NULL;
mSocket = CFSocketCreate(
NULL,
PF_INET,
SOCK_DGRAM,
IPPROTO_UDP,
kCFSocketDataCallBack,
SocketCallback,
&socketContext);
if(NULL != mSocket) {
runLoopSource = CFSocketCreateRunLoopSource(NULL, mSocket, 0);
if(NULL != runLoopSource) {
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopCommonModes);
CFDataRef addressData = CFDataCreateWithBytesNoCopy(NULL,
(UInt8 *)&myAddress, sizeof(struct sockaddr_in), kCFAllocatorNull);
CFSocketError error = CFSocketSetAddress(mSocket,
addressData);
mConnected = (kCFSocketSuccess == error);
CFRelease(addressData);
addressData = NULL;
}
}
CFRunLoopRun();
[autoreleasePool release];
}
void SocketCallback(
CFSocketRef s,
CFSocketCallBackType callbackType,
CFDataRef address,
const void *data,
void *info
)
{
XPlaneDataThread *threadObject = (XPlaneDataThread *) info;
if(NULL != info && NULL != data) {
XPlaneDataPacket dataPacket((CFDataRef) data);
[threadObject extractDataFromPacket: dataPacket];
}
}
const void *XPlaneDataRetainCallback(const void *info)
{
if(info != NULL) {
XPlaneDataThread *objectPtr = (XPlaneDataThread *) info;
[objectPtr retain];
}
return info;
}
void XPlaneDataReleaseCallback(const void *info)
{
if(info != NULL) {
XPlaneDataThread *objectPtr = (XPlaneDataThread *) info;
[objectPtr release];
}
}
CFStringRef XPlaneDataCopyDescriptionCallback(const void *info)
{
CFStringRef retVal = NULL;
if(info != NULL) {
XPlaneDataThread *objectPtr = (XPlaneDataThread *) info;
retVal = (CFStringRef) [objectPtr description];
// Core foundation returns info from a "copy" method
// with a ref count.
CFRetain(retVal);
}
return retVal;
}
On Saturday, June 7, 2003, at 05:53 PM, Ryan McGann wrote:
>
Is there any special trick to receiving UDP broadcasts? When I run the
>
following code, I get nada. But I can see the UDP broadcast packets
>
coming in via tcpdump. The packets are being sent by an SNMP aware
>
router. I am on the same subnet as the router (it's my DHCP server),
>
and I am running the process as root. BTW, I tried using recvfrom
>
instead of CFSocket and get the same results (or lack thereof).
>
>
Thanks for any insight you can give.
>
>
#include <netdb.h>
>
#include <sys/types.h>
>
#include <sys/socket.h>
>
#include <netinet/in.h>
>
#include <arpa/inet.h>
>
#include <CoreFoundation/CoreFoundation.h>
>
>
void SNMPSocketCallBack( CFSocketRef, CFSocketCallBackType, CFDataRef,
>
const void *, void * );
>
>
int main( int argc, int argv )
>
{
>
int theSocket;
>
struct sockaddr_in theAddress;
>
CFSocketContext theContext = { 0, NULL, NULL, NULL, NULL };
>
CFSocketRef theSocketRef;
>
CFRunLoopSourceRef theRLSourceRef;
>
>
theAddress.sin_addr.s_addr = INADDR_ANY;
>
theAddress.sin_len = 0;
>
theAddress.sin_family = AF_INET;
>
theAddress.sin_port = htons( 162 );
>
>
theSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
>
>
bind( theSocket, (struct sockaddr *) &theAddress, sizeof( theAddress
>
) );
>
>
theSocketRef = CFSocketCreateWithNative( NULL, theSocket,
>
kCFSocketReadCallBack, SNMPSocketCallBack, &theContext );
>
theRLSourceRef = CFSocketCreateRunLoopSource( NULL, mSNMPSocketRef, 0
>
);
>
>
CFRunLoopAddSource( CFRunLoopGetCurrent(), theRLSourceRef,
>
kCFRunLoopDefaultMode );
>
CFRelease( theRLSourceRef );
>
}
>
>
void
>
SNMPSocketCallBack(
>
CFSocketRef inSocketRef,
>
CFSocketCallBackType inCallBack,
>
CFDataRef inAddressRef,
>
const void *inDataRef,
>
void *inInfo )
>
{
>
printf( "SNMPSocketCallBack called\n" );
>
}
>
_______________________________________________
>
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.
_______________________________________________
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.