0

I'm trying to broadcast via UDP. I included the CFNetworking framwork and added AsyncUDPSocket to my project.

#import "AsyncUdpSocket.h"

I then created a socket as:

    broadcastSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];

and I send data as:

[self.broadcastSocket enableBroadcast:YES error:&error];
[self.broadcastSocket sendData:[@"hello" dataUsingEncoding:NSASCIIStringEncoding] toHost:@"255.255.255.255" port:5538 withTimeout:10 tag:1];

But when I send data, AsyncUdpSocket crashes with EXC_BAD_ACCESS in

- (CFSocketRef)socketForPacket:(AsyncSendPacket *)packet

Specifically at

return ([packet->address length] == sizeof(struct sockaddr_in)) ? theSocket4 : theSocket6;

OR it also sometimes crashes in:

- (void)doSend:(CFSocketRef)theSocket

at

const void *buf  = [theCurrentSend->buffer bytes];

Is there something wrong with how I am making my socket?

Beaker
  • 1,633
  • 13
  • 22

2 Answers2

1

Ah, I see now. I updated to the latest version of AsyncUDPSocket and it requires ARC. There is a warning that it issues that wasn't popping up in my debugger, which is another problem. However, when looking at the source code, it is on line 11. Doh!

Added ARC flag (-fobjc-arc) and it runs fine!

Beaker
  • 1,633
  • 13
  • 22
  • Thanks a lot, you save my time. I always get error at this line 'const void *buf = [theCurrentSend->buffer bytes];' – majorl3oat Apr 01 '14 at 09:39
0

You created the socket, but did you create packet?

Seth Noble
  • 3,233
  • 19
  • 31
  • You should be able to simply use the sendData function of AsyncUdpSocket to send a packet. It doesn't need to be manually constructed. – Beaker Sep 21 '12 at 00:49
  • The code snippet is incomplete, so it is not clear that when you reference `packet` that it has actually been initialized at that point. If it hasn't been initialized, or if it has been released, that would account for the crash. – Seth Noble Sep 21 '12 at 14:25
  • I'm using an instance of AsyncUdpSocket. You don't need to construct packets manually. – Beaker Sep 23 '12 at 05:19