Re: Listen on a UDP Port
Re: Listen on a UDP Port
- Subject: Re: Listen on a UDP Port
- From: "Bridger Maxwell" <email@hidden>
- Date: Thu, 14 Feb 2008 13:35:48 -0700
Hey, Thanks for the help! The code compiles perfectly. One more question
though, if you would. Does this code:
- (void)receiveMessage:(NSNotification *)notification
{
NSData *messageData = [[notification userInfo]
objectForKey:NSFileHandleNotificationDataItem];
..........
[messageData getBytes:&s_id range:NSMakeRange(offset, sizeof(s_id))];
offset += sizeof(s_id);
......
}
still work to get the data that was received? I seem to be getting only
0's.
Thank You,
Bridger Maxwell
On Thu, Feb 14, 2008 at 8:55 AM, Hamish Allan <email@hidden> wrote:
> Here's some code that may suit your needs (warning: composed in mail
> client). I'm assuming from your code that fileHandle is a member
> variable; so are cfSocket and cfSource. NB this approach keeps
> notifying until you call CFRunLoopRemoveSource(), as opposed to the
> acceptConnectionInBackgroundAndNotify approach where you have to keep
> setting that up.
>
> static void socketCallback(CFSocketRef cfSocket, CFSocketCallBackType
> type, CFDataRef address, const void *data, void *userInfo)
> {
> [[NSNotificationCenter defaultCenter]
> postNotificationName:@"NSFileHandleReadCompletionNotification"
> object:(id)userInfo];
> }
>
> - (id)initWithPortNumber:(int)portNumber
> {
> self = [super init];
> if (self)
> {
> int sockHandle = socket(AF_INET, SOCK_DGRAM, 0);
> fileHandle = [[NSFileHandle alloc]
> initWithFileDescriptor:sockHandle closeOnDealloc:YES];
> struct sockaddr_in sa;
> sa.sin_family = AF_INET;
> sa.sin_addr.s_addr = htonl(INADDR_ANY);
> sa.sin_port = htons(portNumber);
> bind(sockHandle, (struct sockaddr *)&sa, sizeof(sa));
> CFSocketContext socketContext = {0, (void *)fileHandle, NULL, NULL,
> NULL};
> cfSocket = CFSocketCreateWithNative(NULL, sockHandle,
> kCFSocketReadCallBack, socketCallback, &socketContext); // CFSocketRef
> cfSource = CFSocketCreateRunLoopSource(NULL, cfSocket, 0); //
> CFRunLoopSourceRef
> CFRunLoopAddSource(CFRunLoopGetCurrent(), cfSource,
> kCFRunLoopDefaultMode);
> }
> return self;
> }
>
> - (void)dealloc
> {
> CFRunLoopRemoveSource(CFRunLoopGetCurrent(), cfSource,
> kCFRunLoopDefaultMode);
> CFRelease(cfSource);
> CFRelease(cfSocket);
> [fileHandle release];
> [super dealloc];
> }
>
> Best wishes,
> Hamish
>
> On Thu, Feb 14, 2008 at 8:45 AM, Bridger Maxwell <email@hidden>
> wrote:
> > Hello, I know the topic of UDP sockets must come up quite a bit. I
> know
> > because I just spent a while searching the list's history. Although I
> read
> > a ton of posts on UDP sockets and how they may or may not be
> implemented
> > easily in a Cocoa networking wrapper, I still can't make heads or tails
> of
> > the situation. My problem is really quite simple. I would like to
> listen
> > on a socket for incoming UDP packets. I have the code written, but it
> is
> > listening on a TCP socket. Could I change it to listen on UDP without
> > affecting the other functions (i.e. receiveMessage)? I am still very
> new at
> > Cocoa, and most of this code is pulled from an example I found online.
> Any
> > hints would be appreciated. Does NSSocketPort even support UDP?
> >
> >
> > - (id)initWithPortNumber:(int)portNumber
> >
> > {
> >
> > if( self = [super init] ) {
> >
> > NSSocketPort *socketPort = [[NSSocketPort alloc] initWithTCPPort
> > :portNumber];
> >
> > int fd = [socketPort socket];
> >
> > fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
> >
> > closeOnDealloc:YES];
> >
> > NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
> >
> > [nc addObserver:self
> >
> > selector:@selector(receiveMessage:)
> >
> > name:NSFileHandleReadCompletionNotification
> >
> > object:fileHandle];
> >
> > [fileHandle acceptConnectionInBackgroundAndNotify];
> >
> > return self;
> >
> > } else {
> >
> > return nil;
> >
> > }
> >
> > }
> >
> >
> >
> > Thank You,
> >
> > Bridger Maxwell
> > _______________________________________________
> >
> > Cocoa-dev mailing list (email@hidden)
> >
> > Please do not post admin requests or moderator comments to the list.
> > Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> >
> > Help/Unsubscribe/Update your Subscription:
> >
> > This email sent to email@hidden
> >
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden