0

我正在尝试在我的 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] = "1\n";

    NSData *data = [NSData dataWithBytes: &buffer length: sizeof(buffer)];
    [socket writeData:data withTimeout:10 tag:1];

我已经包含了框架依赖项:Security 和 CFNetwork,并在我的课程中包含了各自的委托。我需要任何其他配置才能使用它吗?

当我运行这个例子时,我得到这个错误:

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

它发生在 GCDAsyncSocket.m 的这一行

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

1 回答 1

0

尝试使用它来转换你的数据,从字符串到数据

    +(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] = {'\0','\0','\0'};
    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;
}
于 2012-08-23T15:37:12.403 回答