Re: Listen on a UDP Port
Re: Listen on a UDP Port
- Subject: Re: Listen on a UDP Port
- From: "Hamish Allan" <email@hidden>
- Date: Thu, 14 Feb 2008 21:42:01 +0000
On Thu, Feb 14, 2008 at 8:35 PM, Bridger Maxwell <email@hidden> 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
_______________________________________________
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