Skip navigation.
 
mlRe: Listen on a UDP Port
FROM : Hamish Allan
DATE : Thu Feb 14 22:42:01 2008

On Thu, Feb 14, 2008 at 8:35 PM, Bridger Maxwell <<email_removed>> wrote:

> Hey,
>  Thanks for the help! The code compiles perfectly.  One more question
> though, if you would.  Does this code:
> [...]
> still work to get the data that was received?  I seem to be getting only
> 0's.
>
> Thank You,
>    Bridger Maxwell


Oops, no. I was thinking that acceptConnectionInBackgroundAndNotify
would just tell you when the connection was made and that you'd have
to do the receiving yourself. To make it work with your code, you'd
want to change the socketCallback to something like:

// change this to something lower if you know your messages will be smaller
#define MAX_UDP_DATAGRAM_SIZE 65507

static void socketCallback(CFSocketRef cfSocket, CFSocketCallBackType
type, CFDataRef address, const void *data, void *userInfo)
{
  u_char msg[MAX_UDP_DATAGRAM_SIZE];
  struct sockaddr_in from_addr;
  socklen_t addr_len = sizeof(struct sockaddr_in);
  size_t n = recvfrom(CFSocketGetNative(cfSocket), (void *)&msg,
MAX_UDP_DATAGRAM_SIZE, 0, (struct sockaddr *)&from_addr, &addr_len);
[[NSNotificationCenter defaultCenter]
postNotificationName:@"NSFileHandleReadCompletionNotification"
object:[NSDictionary dictionaryWithObject:[NSData dataWithBytes:&msg
length:(n * sizeof(u_char))]
forKey:@"NSFileHandleNotificationDataItem"];
}

This code discards the information about who the message was received
from; I don't know whether you get that information with the original
TCP listener, but it would be easy to add it back in using the
from_addr variable.

Best wishes,
Hamish

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