AsyncUdpSocket multicast listener

Discussion in 'Mac Programming' started by eddieford, Feb 13, 2012.

  1. eddieford, Feb 13, 2012
    Last edited by a moderator: Feb 13, 2012

    macrumors newbie

    Feb 13, 2012
    I am having issues implementing asyncUDPSocket.

    I need to listen to beacon messages sent to port 9131 that is being broadcast to via UDP.

    I am using the open source AsyncUDPSocket to attempt to do this. I know the packets are being sent to the network via the use of wireshark. My class is acting as the delegate to receive packets as well. (I tried setting the timeout to 10, and my class's didNotReceiveDataWithTag method was called) confirming the delegate setup.

    Thanks In Advance:

    Code Snippets:
    // This will fire with a timeout other than -1 is specified proving the delegate works...
    - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error
         NSLog(@"Did not receive within the timeout specs...");
    - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data 
               fromHost:(NSString *)host 
        NSString *theLine=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];  //Convert the UDP data to an NSString
        NSLog(@"%@", theLine);
        [_mySocket receiveWithTimeout:-1 tag:1];  //Listen for the next UDP packet to arrive...which will call this method again in turn.
        return YES;  //Signal that we didn't ignore the packet.
    - (void)viewDidLoad
        [super viewDidLoad];
    NSError *err;
    // _mySocket is an AsyncUdpSocket property of my class...
    _mySocket       = [[AsyncUdpSocket alloc] initWithDelegate:self];
    [_mySocket setDelegate:self];
    [_mySocket bindToPort:9131 error:&err];
    [_mySocket joinMulticastGroup:@"" error:&err];
    if (err)
            NSLog(@"%@", err);
    [_mySocket receiveWithTimeout:-1 tag:0];

Share This Page