尝试在 iOS 5.1 上使用 GCDAsyncSocket 写入数据时出错



我正在尝试在我的iOS应用程序中使用GCDAsyncSocket。我一直在遵循CocoaAsyncSocket的wiki中提供的所有步骤。这是我正在做的事情:

 GCDAsyncSocket socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    NSError *err = nil;
    if (![socket connectToHost:@"192.168.0.129" onPort:2811 error:&err]) // Asynchronous!
    {
        // If there was an error, it's likely something like "already connected" or "no delegate set"
        NSLog(@"I goofed: %@", err);
    }
    uint8_t buffer[2] = "1n";
    NSData *data = [NSData dataWithBytes: &buffer length: sizeof(buffer)];
    [socket writeData:data withTimeout:10 tag:1];

我已经包含了框架依赖关系:Security&CFNetwork,并将各自的代表包含在我的类中。我需要任何其他配置才能使用它吗?

当我运行此示例时,出现此错误:

[NSMallocBlock bytes]:发送到实例0x6b7abe0的无法识别的选择器 "NSInvalidArgumentException",原因:"-[NSMallocBlock bytes]:发送到实例 0x6b7abe0 的无法识别的选择器

它发生在GCDAsyncSocket.m的这一行上。

const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes] + currentWrite->bytesDone;

尝试使用它来转换数据,从字符串转换为数据

    +(NSData *) DataToHex: (NSString *) string
{
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    int stringLength = string.length;
    NSMutableData *hexStringArray = [[NSMutableData alloc] init];
    [hexStringArray setLength:0];
    unsigned char whole_byte;
    char byte_chars[3] = {'','',''};
    int i;
    for (i=0; i < stringLength/2; i++) 
    {
        byte_chars[0] = [string characterAtIndex:i*2];
        byte_chars[1] = [string characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [hexStringArray appendBytes:&whole_byte length:1]; 
    }                                                                       //this is auto way
    return hexStringArray;
}

相关内容

  • 没有找到相关文章

最新更新