ios - When I use AsyncUdpSocket object to send IP packet, It is not always working -
i using cocoaasyncsocket. got there. asyncudpsocket object me send ip packet when init object. when use [engineobject startsession] call function in other controller, function working, asyncudpsocket object won't send ip packet out. won't call (or trigger) delegate method: didsenddatawithtag or didnotsenddatawithtag.....
what did wrong?
.h
#import <foundation/foundation.h> #import "asyncudpsocket.h" @interface engine : nsobject{ asyncudpsocket *asyncudpsocket; } @property (atomic, strong) asyncudpsocket *asyncudpsocket; - (id) init; - (bool) startsession; - (void) dosomething; @end
.m
@implementation engine - (id) init { [self dosomething]; //<-----<< can send ip packet out self = [super init]; [self dosomething]; //<-----<< can send ip packet out, wrong bind source port if (self){ } return self; } - (bool) startsession{ [self dosomething]; //<-----<< won't send ip packet out [self oxox]; return yes; } - (void) oxox{ [self dosomething]; //<-----<< won't send ip packet out } - (void) dosomething{ nserror *socketerror=nil; asyncudpsocket = [[asyncudpsocket alloc] initwithdelegate:self]; if (![asyncudpsocket bindtoport:7701 error:&socketerror]){ nslog(@"rasengine: bind port fail"); } [asyncudpsocket enablebroadcast:no error:&socketerror]; uint8_t signalbytes[] = {0x07, 0x07, 0x01, 0x06, 0x12, 0x34, 0x56, 0x78}; nsdata *signaldata = [nsdata datawithbytes:signalbytes length:8]; [asyncudpsocket senddata:signaldata //<--------<< called every time, doesn't send out. tohost:@"192.168.16.18" port:9902 withtimeout:-1 tag:0]; } #pragma mark - #pragma mark asyncudpsocket delegate udp - (void)onudpsocket:(asyncudpsocket *)sock didsenddatawithtag:(long)tag{ nslog(@"udp engine: onudpsocket:didsenddatawithtag:%ld", tag); } - (void)onudpsocket:(asyncudpsocket *)sock didnotsenddatawithtag:(long)tag duetoerror:(nserror *)error{ nslog(@"udp engine: onudpsocket:didnotsenddatawithtag:%ld", tag); } @end
after 1 week fight, found problem caused runloop. using gcdasyncudpstoket replace asynvudpsocket, problem solved!
gcdasyncudpsocket *gcdasyncudpsocket = [[gcdasyncudpsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue()];
Comments
Post a Comment