Advertisement
Advertisement

新足迹

 找回密码
 注册
新足迹 门户 IT专业论坛 查看内容

搞不定了乱投医一下。iOS 开发,有人用过CocoaAsyncSocket吗?

2013-8-16 17:37| 发布者: jacey | 查看: 1991| 原文链接

再init对象时还能发送UDP包,到了调用的时候就没反应了。function跑起来没问题,但是asyncUdpSocket的对象就是不发送UDP包。

Stackoverflow上都没人搭理我。

https://groups.google.com/forum/ ... csocket/GMLVDjzinNQ CocoaAsyncSocket 的google Group也没人回复。

抓狂了!!!!

.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

#import "Engine.h"
@implementation Engine

- (id) init {
    [self doSomething];  //<-----<< It can send ip packet out

    self = [super init];

    [self doSomething];  //<-----<< It can send ip packet out, with wrong bind source port
    if (self){

    }
    return self;
}

- (BOOL) startSession{
    [self doSomething]; //<-----<< It won't send any ip packet out
    [self oxox];
    return YES;
}

- (void) oxox{

    [self doSomething]; //<-----<< It won't send any ip packet out

}

- (void) doSomething{
    NSError *socketError=nil;
    asyncUdpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
    if (![asyncUdpSocket bindToPort:7701
                              error:&socketError]){
        NSLog(@"RASEngine:    Bind to 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   //<--------<< It is called every time, but doesn't send anything out.  
                      toHost"192.168.16.18"
                        port:9902
                 withTimeout:-1
                         tag:0];
}

#pragma mark -
#pragma mark AsyncUdpSocket Delegate for UDP
- (void)onUdpSocketAsyncUdpSocket *)sock didSendDataWithTaglong)tag{
    NSLog(@"UDP Engine:    onUdpSocket:didSendDataWithTag:%ld", tag);   
}


- (void)onUdpSocketAsyncUdpSocket *)sock didNotSendDataWithTaglong)tag dueToErrorNSError *)error{
    NSLog(@"UDP Engine:    onUdpSocket:didNotSendDataWithTag:%ld", tag);   
}

@end
Advertisement
Advertisement


Advertisement
Advertisement
返回顶部