Skip navigation.
 
mlRe: Listen on a UDP Port
FROM : Bridger Maxwell
DATE : Thu Feb 14 21:35:48 2008

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_removed>> 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_removed>>
> 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_removed>)
> >
> >  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:
> >  http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
> >
> >  This email sent to <email_removed>
> >
>

Related mailsAuthorDate
mlListen on a UDP Port Bridger Maxwell Feb 14, 09:45
mlRe: Listen on a UDP Port Hamish Allan Feb 14, 16:55
mlRe: Listen on a UDP Port Bridger Maxwell Feb 14, 21:35
mlRe: Listen on a UDP Port Hamish Allan Feb 14, 22:42